Handling multiple file uploads in MVC - How can I

2019-09-17 04:53发布

问题:

I have a form in my MVC 3 application that allows the user to optionally upload 2 files (or one or the other) and am looking for a way to tell which file is which if only one of the two files is uploaded (e.g. extracting the element ID from the input). The files serve very different purposes and will be saved to different folders on the server. Both files could potentially be the same file type (Word, PDF, etc), so file extension would not be a reliable way to tell them apart. Is there a way this can be done without making the user give the files a certain filename or something unreliable like that? Here is an example of what I'm trying to do (I know my if statements are not proper syntax. They are just to clarify what I want to do). Thanks everyone!

 public ActionResult SaveProfile(IEnumerable<HttpPostedFileBase> files)
        .....
        foreach (var file in files)           
        {                 
            if (file has element id "file1") {
                 file.SaveAs(Server.MapPath("~/Folder1/" + file.FileName));
            }
            if (file has element id "file2") {
                 file.SaveAs(Server.MapPath("~/Folder2/" + file.FileName));
            }
        }
 }

回答1:

if you have constant numbers of file inputs, just gave them different names. And change your post action args like this:

public ActionResult SaveProfile(HttpPostedFileBase file_name1, HttpPostedFileBase file_name2)


回答2:

I strongly suggest to look ELFinder.js and ELFinder.net.

http://elfinder.org/

https://elfinder.codeplex.com/