【问题标题】:C# MVC - uploading multiple large files creating byte arrayC# MVC - 上传多个大文件创建字节数组
【发布时间】:2018-03-19 10:04:31
【问题描述】:

有人可以通过将大型文档转换为字节数组来帮助我上传(多个)大型文档吗?

我的代码目前在没有字节数组的情况下工作,但如果文档很大,它当然会失败。

控制器:

[HttpPost] [验证AntiForgeryToken] public ActionResult Create(Invoice invoice) { 如果(模型状态。IsValid) { 列表文件详细信息 = 新列表(); for (int i = 0; i

            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                FileDetail fileDetail = new FileDetail()
                {
                    FileName = fileName,
                    Extension = Path.GetExtension(fileName),
                    Id = Guid.NewGuid()
                };
                fileDetails.Add(fileDetail);

                var path = Path.Combine(Server.MapPath("~/App_Data/Upload/"),
                fileDetail.Id + fileDetail.Extension);
                file.SaveAs(path);

            }
        }

        invoice.FileDetails = fileDetails;
        db.Invoices.Add(invoice);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(invoice);
}

和表单元素:

任何帮助将不胜感激。

【问题讨论】:

    标签: c# arrays file upload


    【解决方案1】:

    排序!

    [HttpPost] [验证AntiForgeryToken] public ActionResult Create(Invoice invoice) { 如果(模型状态。IsValid) { 列表文件详细信息 = new List();

                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase httpPostedFileBase = Request.Files[i];
    
                    if (httpPostedFileBase != null)
                    {
                        Stream stream = httpPostedFileBase.InputStream;
                        BinaryReader bReader = new BinaryReader(stream);
                        byte[] bytes = bReader.ReadBytes((Int32)stream.Length);
                    }
    
                    HttpPostedFileBase postedFileBase = Request.Files[i];
    
                    if (postedFileBase != null)
                    {
                        var fileName = Path.GetFileName(postedFileBase.FileName);
    
                        FileDetail fileDetail = new FileDetail()
                        {
                            FileName = fileName,
                            Extension = Path.GetExtension(fileName),
                            Id = Guid.NewGuid()
                        };
                        fileDetails.Add(fileDetail);
                        //Save the Byte Array as File.
                        var path = Path.Combine(Server.MapPath("~/App_Data/Upload/"),
                            fileDetail.Id + fileDetail.Extension);
                        postedFileBase.SaveAs(path);
                        postedFileBase.InputStream.Flush();
                    }
                }
    
                invoice.FileDetails = fileDetails;
                db.Invoices.Add(invoice);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(invoice);
        }
    

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2014-04-04
      • 1970-01-01
      相关资源
      最近更新 更多