简体   繁体   English

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

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

Could someone help me upload large documents (multiple) by converting those to byte array? 有人可以通过将大型文档转换为字节数组来帮助我上传大型文档(多个)吗?

My code is currently working without the byte array but it fails of course if the documents are large. 我的代码当前没有字节数组,但是如果文档很大,它当然会失败。

Controller: 控制器:

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

and the form element: 和表单元素:

Any help will be much appreciated. 任何帮助都感激不尽。

Sorted! 排序!

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