【问题标题】:ASP.NET Razor File UploadASP.NET Razor 文件上传
【发布时间】:2018-01-21 16:59:55
【问题描述】:

我在 MS 网站上关注此示例,以使用 Razor 和 C# 进行文件上传。

如果我有多个文件上传按钮,C# 代码如何知道上传文件来自哪个按钮?根据上传文件的按钮,我会将文件保存到特定文件夹。

https://docs.microsoft.com/en-us/aspnet/web-pages/overview/data/working-with-files

@using Microsoft.Web.Helpers;
    @{
        var fileName = "";
        if (IsPost) {
            var fileSavePath = "";
            var uploadedFile = Request.Files[0];
            fileName = Path.GetFileName(uploadedFile.FileName);
            fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" +
              fileName);
            uploadedFile.SaveAs(fileSavePath);
        }
    }
    <!DOCTYPE html>
    <html>
        <head>
        <title>FileUpload - Single-File Example</title>
        </head>
        <body>
        <h1>FileUpload - Single-File Example</h1>
        @FileUpload.GetHtml(
            initialNumberOfFiles:1,
            allowMoreFilesToBeAdded:false,
            includeFormTag:true,
            uploadText:"Upload")
        @if (IsPost) {
            <span>File uploaded!</span><br/>
        }
        </body>
    </html>

【问题讨论】:

    标签: c# asp.net razor file-upload


    【解决方案1】:

    这样做的方法是将按钮命名相同,但赋予它们不同的值。然后你可以做一个case语句并根据值来指导逻辑。

    剃须刀

    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    
        <input type="file" name="FirstUpload" />
        <input type="submit" name="submitID" id="submitID" value="Upload1" />
    
        <input type="file" name="SecondUpload" />
        <input type="submit" name="submitID" id="submitID" value="Upload2" />
    
    }
    

    控制器

    public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Index(FormCollection collection)
            {
                string btn = Request.Params["submitID"];
    
                switch (btn)
                {
                    case "Upload1":
    
                        for (int i = 0; i < Request.Files.Count; i++)
                        {
                            var file = Request.Files[i];
                        }                      
                        break;
    
                    case "Upload2":
    
                        for (int i = 0; i < Request.Files.Count; i++)
                        {
                            var file = Request.Files[i];
                        }
                        break;
                }
    
                return View();
            }
    

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,最后得到了以下代码:
      Razor

      //BeginForm and other staff
      @foreach (var d in Model.Types)
      {
          <input type="file" name="doc:@d.Id:upload" />//@d.Id is the key point
          <button formenctype="multipart/form-data"
            type="submit" formaction="/MyController/uploaddoc" 
            name="typeid" formmethod="post" value="@d.Id">//this way I send Id to controller
             Upload
          </button>
      }
      

      我的控制器

      [HttpPost]
      [ValidateAntiForgeryToken]
      [Route("mycontroller/uploaddoc")]//same as formaction
      public async Task<ActionResult> UploadDoc(FormCollection data, int typeid)
      {
          //restore inpput name
          var fl = Request.Files["doc:" + typeid.ToString() + ":upload"];
          if (fl != null && fl.ContentLength > 0)
          {
              var path = Server.MapPath("~/app_data/docs");
              var fn = Guid.NewGuid().ToString();//random name
              using (FileStream fs = System.IO.File.Create(Path.Combine(path, fn)))
              {
                  await fl.InputStream.CopyToAsync(fs);
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        • 2012-03-13
        • 2020-07-19
        • 2013-11-11
        • 2014-07-23
        • 1970-01-01
        相关资源
        最近更新 更多