Handling multiple file uploads in MVC - How can I know which file is which?

最后都变了- 提交于 2019-12-25 12:22:25

问题


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/



来源:https://stackoverflow.com/questions/21781721/handling-multiple-file-uploads-in-mvc-how-can-i-know-which-file-is-which

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!