asp.net-mvc 使用 ASP.Net MVC 上传文件 - 获取名称但没有文件流,我做错了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/297954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 22:53:24  来源:igfitidea点击:

Uploading Files with ASP.Net MVC - get name but no file stream, what am I doing wrong?

asp.net-mvcupload

提问by Jason Diller

I have this form in my view:

我认为有这种形式:

<!-- Bug (extra 'i') right here-----------v  -->
<!-- was: <form method="post" enctype="mulitipart/form-data" action="/Task/SaveFile"> -->
<form method="post" enctype="multipart/form-data" action="/Task/SaveFile">
<input type="file" id="FileBlob" name="FileBlob"/>
<input type="submit"  value="Save"/>
<input type="button" value="Cancel" onclick="window.location.href='/'" />
</form>

And this code in my controller:

我的控制器中的这段代码:

public ActionResult SaveFile( FormCollection forms )
{
   bool errors = false;
   //this field is never empty, it contains the selected filename
   if ( string.IsNullOrEmpty( forms["FileBlob"] ) )
   {
       errors = true;
       ModelState.AddModelError( "FileBlob", "Please upload a file" );
   }
   else
   {
      string sFileName = forms["FileBlob"];
      var file = Request.Files["FileBlob"];
      //'file' is always null, and Request.Files.Count is always 0 ???
      if ( file != null )
      {
         byte[] buf = new byte[file.ContentLength];
         file.InputStream.Read( buf, 0, file.ContentLength );
         //do stuff with the bytes
      }
      else
      {
         errors = true;
         ModelState.AddModelError( "FileBlob", "Please upload a file" );
      }
   }
   if ( errors )
   {
      return ShowTheFormAgainResult(); 
   }
   else
   {
      return View();
   }
}

Based on every code sample I've been able to find, this seems like the way to do it. I've tried with small and large files, with no difference in the result. The form field always contains the filename which matches what I've chosen, and the Request.Files collection is always empty.

根据我能找到的每个代码示例,这似乎是一种方法。我试过小文件和大文件,结果没有区别。表单字段始终包含与我选择的文件名匹配的文件名,Request.Files 集合始终为空。

I don't think it's relevant, but I'm using the VS Development Web Server. AFAIK it supports file uploads the same as IIS.

我认为这无关紧要,但我使用的是 VS Development Web Server。AFAIK 它支持与 IIS 相同的文件上传。

It's getting late and there's a chance I'm missing something obvious. I'd be grateful for any advice.

已经很晚了,我有可能遗漏了一些明显的东西。如有任何建议,我将不胜感激。

回答by Jason Diller

I don't know what the policy is on posting profanity, but here's the problem:

我不知道发布亵渎的政策是什么,但问题是:

enctype="mulitipart/form-data"

The extra iin there stopped the file from uploading. Had to run Fiddler to see that it was never sending the file in the first place.

i那里的额外内容阻止了文件上传。必须运行 Fiddler 才能看到它从一开始就没有发送文件。

It should read:

它应该是:

enctype="multipart/form-data"

回答by Jason

For people who may stumble on to this post in the future, here a great post by Scott Hanselman on the subject: A Back To Basics Case Study: Implementing HTTP File Upload with ASP.NET MVC including Tests and Mocks

对于将来可能偶然发现这篇文章的人,这里是 Scott Hanselman 关于这个主题的一篇很棒的文章:回归基础案例研究:使用 ASP.NET MVC 实现 HTTP 文件上传,包括测试和模拟

回答by Pure.Krome

var file = Request.Files[sFileName];

should be...

应该...

var file = Request.Files["FileBlob"];

that said, Request.Files.Countshould be 1 ... hmmm

也就是说,Request.Files.Count应该是 1 ... 嗯

回答by Todd Smith

Good that you found your error.

很好,你发现了你的错误。

As a side note you'll want a try/catch around the file handling code so you'll know when file permissions etc. are not setup correctly.

作为旁注,您需要在文件处理代码周围进行 try/catch,以便您知道文件权限等何时未正确设置。