[英]How to upload multiple files to server with form post MVC?
I am working on a project of Asp.Net MVC-4. 我正在研究Asp.Net MVC-4的项目。 In my project user post requirements.
在我的项目中用户职位要求。 Post contains title , tags , files etc.File may be multiple and it may be of any type like Video, doc ( ppt, excel, pdf, etc), images etc.
帖子包含标题 , 标签 , 文件等文件可以是多个文件 ,并且可以是视频,doc(ppt,excel,pdf等),图像等任何类型。
My problem is the handling of multiple file upload. 我的问题是多个文件上传的处理。 Now first of all i tell you
现在我首先告诉你
currently how i am handling this : 目前我如何处理这个 :
I am using Jquery FIle Uplaod plugin. 我正在使用Jquery FIle Uplaod插件。 Through this plugin i am uploading file to server sequentially and on server i am saving those file with SessionId .Now when user post there requirement form than i just rename those file with userId.
通过这个插件,我可以按顺序将文件上传到服务器上,并在服务器上使用SessionId保存这些文件。现在,当用户在其中发布需求表格时,我只会使用userId重命名这些文件。
drawback of my approach 我的方法的缺点
First i have to save those files with session id and than i have to rename it with userId. 首先,我必须使用会话ID保存这些文件,然后我必须使用userId将其重命名。 So if i save my file in Window Azure Blobs than in that case for uploading single file i have to do 2 transaction.
因此,如果我将文件保存在Window Azure Blob中,而不是在那种情况下上传单个文件,则必须执行2次事务。 First save the blob with SessionId and than Renaming the blob with userid.
首先使用SessionId保存Blob,然后使用userid重命名Blob。 Which i think result extra processing and extra cost.
我认为这会导致额外的处理和额外的成本。
Now i want to know if there is any approach by which i can upload all file ( with progress bar for individual file [required] ) with form post. 现在,我想知道是否可以通过表单发布上传所有文件( 带有单个文件的进度条[必填] )。 So that user's requirement form (tags, titile etc) with all files go to server together than in that case i will save the user first in the database and than i will save the files with the userId ??
因此,带有所有文件的用户需求表(标签,标题等)将一起发送到服务器,在这种情况下,我将首先将用户保存在数据库中,然后我将使用userId保存文件?
Note: I cannot save File with guid or other. 注意: 我无法使用guid或其他文件保存文件。 UserId is required in the file name to uniquely identify user's files.
文件名中必须提供UserId才能唯一标识用户的文件。
You can upload using following code in controller 您可以在控制器中使用以下代码上传
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
foreach (var file in files) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
Not sure if i completely understand the question, but you could post the model for your user details in the same POST as the file upload (same form?), then on the server: 不知道我是否完全理解这个问题,但是您可以在文件上传的同一POST中发布用于用户详细信息的模型(格式相同?),然后在服务器上发布:
[HttpPost]
public JsonResult AddNewImage(UserModel user)
{
ReturnArgs r = new ReturnArgs();
repo.AddUser(user) // add your user to DB here
SaveImages(user.Id);
r.Status = 200;
r.Message = "OK!";
return Json(r);
}
private void SaveImages(string userid)
{
for (var i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i] as HttpPostedFileBase;
string fileName = userid + "_" + i;
// saving file to DB here, but you can do what you want with
// the inputstream
repo.SaveImage(fileName, file.InputStream, file.ContentType);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.