Looping posted files uploaded by uploadify in MVC3
I am using uploadify to upload multiple files in an MVC 3 app. If I select multiple files and hit Upload, I get the files but one-by-one in the controller. What I want is that the posted files come as a collection so that I can loop through them.
I saw some related post here, but I can't use that since it doesn't use uploadify.
Any idea how to do it?
Razor View code:
<input type="file" id="file_upload" name="file_upload" />
<a onclick="Upload();" style="font:12px Arial; text-decoration: none; outline: none;">Upload</a>
<input type="hidden" id="CanFiles" />
$('#file_upload').uploadify({
'uploader': '/content/uploadify/uploadify.swf',
'script': '/upload/UploadFiles',
'scriptData': { 'auth': auth, 'sid': sid, 'multi': 1 },
'cancelImg': '/content/uploadify/cancel.gif',
'folder': '/content/uploadify/uploads',
'auto': false,
'multi': true,
'simUploadLimit': 1,
'expressInstall': '/content/uploadify/expressInstall.swf',
});
current controller action:
public 开发者_运维知识库ActionResult UploadFiles()
{
foreach (string fileName in Request.Files)
{
//this always get 1 file and action gets called once for each file
}
}
This is what I want:
public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
{
//loop the files posted
}
What you need is a post of multiple files to the same action. In order to do that, it is not necessary to use a library like uploadify. What you can easily do is this : write javascript to hide a file element as soon as the user has chosen a file and create a new file element in its place. That way, you have multiple file elements pointing to different files, all except one is concealed, but present on the page, and when user submits the form, you get all files in the Request.Files array. I will edit this answer and give you a sample soon.
EDIT:
Here is an example :
http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/
It is a little bit basic, you can make it work though.
精彩评论