在处理MVC多文件上传 - 我怎么知道哪些文件是哪个? [英] Handling multiple file uploads in MVC - How can I know which file is which?

查看:153
本文介绍了在处理MVC多文件上传 - 我怎么知道哪些文件是哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形式,我的MVC 3应用程序,允许用户选择上传2个文件(或一个或其他),并正在寻找一种方式来告诉哪个文件是如果只有两个文件之一上传(例如提取从输入元件ID)。这些文件服务非常不同的目的,将被保存到服务器上不同的文件夹。这两个文件可能是相同的文件类型(如Word,PDF等),所以文件扩展名不会是一个可靠的方法来区分它们。有没有一种方法可以在不使用户给文件一定文件名或不可靠的东西那样做呢?这里是什么我试图做一个例子(我知道我的if语句不正确的语法。他们只是澄清我想要做的)。谢谢大家!

 公众的ActionResult SaveProfile(IEnumerable的< HttpPostedFileBase>文件)
        .....
        的foreach(在文件var文件)
        {
            如果(文件有元素ID为文件1){
                 file.SaveAs(使用Server.Mappath(〜/资料夹/+ file.FileName));
            }
            如果(文件有元素ID文件2){
                 file.SaveAs(使用Server.Mappath(〜/ FOLDER2 /+ file.FileName));
            }
        }
 }


解决方案

如果你有文件输入常数,只是给他们不同的名字。并改变你的帖子行动ARGS是这样的:

 公众的ActionResult SaveProfile(HttpPostedFileBase file_name1,HttpPostedFileBase file_name2)

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

解决方案

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)

这篇关于在处理MVC多文件上传 - 我怎么知道哪些文件是哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆