获得上传多个文件的文件名 [英] Get file names from multiple files uploaded

查看:161
本文介绍了获得上传多个文件的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

4文件上传在我的形式。我想,当我点击一个按钮,我得到的文件名,并设置模型,然后将文件上传到服务器。但我不想一定要用4文件,我可以选择同时使用3,2,或1个文件上传。如果我只用2文件上传,文件3和file4将模式仍然空。我必须在我的控制器呢?

任何意见或建议?

型号:

 公共字符串文件1 {获取;设置;}
公共字符串文件2 {获取;设置;}
公共字符串文件3 {获取;设置;}
公共字符串file4将获得{;设置;}

查看:

  @using(Html.BeginForm(行动,控制器,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
    <输入类型=文件名称=文件ID =文件1/>
    <输入类型=文件名称=文件ID =文件2/>
    <输入类型=文件名称=文件ID =文件3/>
    <输入类型=文件名称=文件ID =file4将/>    <输入类型=提交/>
}


解决方案

在您的视图:

注: 在这里,你的模型名称不是必需的。通过行动结果参数,你要通过你的文件。

  @using(Html.BeginForm(文件上传,家,
                    FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
    <输入类型=文件名称=文件1ID =文件1/>
    <输入类型=文件名称=文件2ID =文件2/>
    <输入类型=文件名称=文件3ID =文件3/>
    <输入类型=文件名称=file4将ID =file4将/>    <输入类型=提交值=上传文件ID =btnSubmit按钮/>
}

在你的控制器:

  [HttpPost]
公众的ActionResult文件上传(HttpPostedFileBase文件1,HttpPostedFileBase文件2,HttpPostedFileBase文件3,HttpPostedFileBase file4将)// OR的IEnumerable< HttpPostedFileBase>档
{
    HttpPostedFileBase文件1 = Request.Files [文件1];
    HttpPostedFileBase文件2 = Request.Files [文件2];
    HttpPostedFileBase文件3 = Request.Files [文件3];
    HttpPostedFileBase file4将= Request.Files [file4将];    如果(文件1!= NULL)//同样的文件2,文件3,file4将
    {
        //如果这是真的,那么文件1有文件,
    }    //检查并保存文件1 //同样的文件2,文件3,file4将
    如果(file1.ContentLength大于0)
    {
        字符串文件路径= Path.Combine(HttpContext.Server.MapPath(../上传),
                                       Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(文件路径);
    }    返回查看();
}

有一个关于文件上传在<一的一篇文章href=\"http://stackoverflow.com/questions/19019345/null-exception-in-file-uploading-in-mvc4-razor/19019855#19019855\">$c$cproject.
我没有检查上述code。让我知道,如果发现没有奏效。

I have 4 fileupload in my form. I want when I click a button, I get the file name and set to model, then the file upload to server. But i don't want must use 4 file, I can use 3, 2, or 1 file upload at a time by choice. If I only use 2 file upload, file3 and file4 model still null. what I must do in my controller ?

Any ideas or suggestion?

Model:

public string file1 {get;set;}
public string file2 {get;set;}
public string file3 {get;set;}
public string file4 {get;set;}

View:

@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="files" id="file1" />
    <input type="file" name="files" id="file2" />
    <input type="file" name="files" id="file3" />
    <input type="file" name="files" id="file4" />

    <input type="submit" />
}

解决方案

In Your View :

Note: Here your model names are not required. Through action result parameter you going to pass your files.

@using (Html.BeginForm("FileUpload", "Home",
                    FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file1" id="file1" />
    <input type="file" name="file2" id="file2" />
    <input type="file" name="file3" id="file3" />
    <input type="file" name="file4" id="file4" />

    <input type="submit" value="Upload File" id="btnSubmit" />
}

In your Controller:

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file1, HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4) // OR IEnumerable<HttpPostedFileBase> files
{
    HttpPostedFileBase file1 = Request.Files["file1"];
    HttpPostedFileBase file2 = Request.Files["file2"];    
    HttpPostedFileBase file3 = Request.Files["file3"];
    HttpPostedFileBase file4 = Request.Files["file4"];

    if (file1 != null) // Same for file2, file3, file4
    {
        //If this is True, then file1 has file.,
    }

    // Check and Save the file1 // Same for file2, file3, file4
    if (file1.ContentLength > 0) 
    {
        string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                       Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(filePath);
    }

    return View();
}

There is an article about file uploading in Codeproject. I didn't checked the above code. Let me know if that is not getting worked.

这篇关于获得上传多个文件的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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