繁体   English   中英

C#MVC-上传多个大文件以创建字节数组

[英]C# MVC - uploading multiple large files creating byte array

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

我的代码当前没有字节数组,但是如果文档很大,它当然会失败。

控制器:

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Invoice invoice){if(ModelState.IsValid){List fileDetails = new List(); for(int i = 0; i <Request.Files.Count; i ++){var file = Request.Files [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);
}

和表单元素:

任何帮助都感激不尽。

排序!

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Invoice invoice){if(ModelState.IsValid){List fileDetails = 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);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM