我正在使用HttpPostedFileBase
来上传MVC中的文件。我一次上传5个文件时工作正常。但是当我选择超过5个文件时,即使是6个文件,它也无法正常工作,保存功能也无法正常工作。
这是我的ActionMethod,用于通过上传文件保存其他数据。
ActionMethod
[HttpPost]
public ActionResult AddEditCoffinCasketItem(CoffinCasketItemModel model)
{
if (ModelState.IsValid)
{
// Some code
if (coffinData.ID > 0)
{
if (model.Image != null)
{
coffinData.ThumbImageUrl = CommonHelper.UploadFile(SystemEnum.EntityType.Coffin.ToString(), model.Image);
}
if (model.Image360 != null)
{
coffinData.Image360FileSize = CommonHelper.ToKiloBytes(model.Image360.ContentLength);
coffinData.Image360Url = CommonHelper.UploadFile(SystemEnum.EntityType.Coffin.ToString(), model.Image360);
}
}
else
{
coffinData.ThumbImageUrl = CommonHelper.UploadFile(SystemEnum.EntityType.Coffin.ToString(), model.Image);
if (model.Image360 != null)
{
coffinData.Image360FileSize = CommonHelper.ToKiloBytes(model.Image360.ContentLength);
coffinData.Image360Url = CommonHelper.UploadFile(SystemEnum.EntityType.Coffin.ToString(), model.Image360);
}
if (model.IsBuildYourOwn == true)
{
coffinData.TopImageUrl = CommonHelper.UploadFile(SystemEnum.EntityType.Coffin.ToString(), model.TopImage);
coffinData.BottomImageUrl = CommonHelper.UploadFile(SystemEnum.EntityType.Coffin.ToString(), model.BottomImage);
}
}
int id = this.coffinService.SaveCoffin(coffinData);
foreach (HttpPostedFileBase item in model.LargeImage)
{
if (item != null)
{
CoffinCasketImage coffinImage = new CoffinCasketImage();
coffinImage.LargeImageUrl = CommonHelper.UploadFile(SystemEnum.EntityType.Coffin.ToString(), item);
int coffinImageId = this.coffinService.SaveCoffinLargeImage(coffinImage);
}
}
this.TempData["msg"] = string.Format(Messages.MessageTypeSave, ExtentionMethods.GetEnumDescription(SystemEnum.Category.Coffin.GetHashCode() == model.CategoryId ? SystemEnum.EntityType.CoffinItem : SystemEnum.EntityType.CasketItem));
this.TempData["status"] = SystemEnum.MessageType.success.GetHashCode();
return this.RedirectToAction("Index", "Coffin", new { categoryId = model.CategoryId });
}
}
UploadFile方法
public static string UploadFile(string uploadDir, HttpPostedFileBase file)
{
string fileName = string.Empty;
try
{
if (file != null && file.ContentLength > 0)
{
DateTime dt = DateTime.Now;
string second = dt.Day + "_" + dt.Month + "_" + dt.Year + "_" + dt.Hour + "_" + dt.Minute + "_" + dt.Second;
fileName = Path.GetFileNameWithoutExtension(file.FileName) + second + Path.GetExtension(file.FileName);
string serverPath = HttpContext.Current.Server.MapPath(@"~/Upload/");
string filePath = Path.Combine(serverPath, uploadDir, fileName);
if (!Directory.Exists(Path.Combine(serverPath, uploadDir)))
{
Directory.CreateDirectory(Path.Combine(serverPath, uploadDir));
}
file.SaveAs(filePath);
}
}
catch (Exception ex)
{
throw ex;
}
return "/Upload/" + uploadDir + "/" + fileName;
}
的Web.config
<system.web>
// other stuff
<httpRuntime targetFramework="4.5.1" maxRequestLength ="2097151"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!-- Limit file uploads to 55MB -->
<requestLimits maxAllowedContentLength="57671680"/>
</requestFiltering>
</security>
</system.webServer>
如何处理这个问题?
答案 0 :(得分:0)
我过去遇到过同样的问题。现在我正在使用Http-Multipart-Data-Parser,其中所有图像都合并在单个流对象中。
请参阅此页:https://github.com/Vodurden/Http-Multipart-Data-Parser
public string UploadImages(Stream imgstram)
{
var parser = new MultipartFormDataParser(imgstram);
foreach (var file in parser.Files)
{
//file => stream of single image.
//convert stream to image
}
}