在MVC中处理多个文件上传 - 我如何知道哪个文件是哪个?

时间:2021-12-25 19:37:01

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!

我在我的MVC 3应用程序中有一个表单,允许用户可选地上传2个文件(或一个或另一个),并且正在寻找一种方法来告诉哪个文件是哪个(如果只上传了两个文件中的一个)(例如,提取来自输入的元素ID)。这些文件的用途非常不同,将保存到服务器上的不同文件夹中。这两个文件可能是相同的文件类型(Word,PDF等),因此文件扩展名不是一种可靠的方式来区分它们。有没有办法可以做到这一点,而不是让用户给文件一个特定的文件名或类似的东西不可靠?这是我正在尝试做的一个例子(我知道我的if语句不是正确的语法。它们只是为了澄清我想做什么)。感谢大家!

 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));
            }
        }
 }

2 个解决方案

#1


0  

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

如果你有恒定数量的文件输入,只给它们不同的名字。并改变你的post action args:

public ActionResult SaveProfile(HttpPostedFileBase file_name1, HttpPostedFileBase file_name2)

#2


0  

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

我强烈建议你看看ELFinder.js和ELFinder.net。

http://elfinder.org/

https://elfinder.codeplex.com/

#1


0  

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

如果你有恒定数量的文件输入,只给它们不同的名字。并改变你的post action args:

public ActionResult SaveProfile(HttpPostedFileBase file_name1, HttpPostedFileBase file_name2)

#2


0  

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

我强烈建议你看看ELFinder.js和ELFinder.net。

http://elfinder.org/

https://elfinder.codeplex.com/