文件上传与ASP.Net MVC - 得到的名字,但没有文件流,我究竟做错了什么? [英] Uploading Files with ASP.Net MVC - get name but no file stream, what am I doing wrong?

查看:99
本文介绍了文件上传与ASP.Net MVC - 得到的名字,但没有文件流,我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种形式在我看来:

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>

这code在我的控制器:

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

根据每code示例中,我已经能够找到,这似乎是这样做的方式。我试着小型和大型文件,在结果没有差异。窗体字段总是包含的文件名相匹配我选择什么,而Request.Files集合总是空的。

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.

我不认为这是相关的,但我使用的VS开发Web服务器。 AFAIK它支持文件上传一样IIS。

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.

这是越来越晚,有一个机会,我失去了一些东西明显。我会的任何意见表示感谢。

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

推荐答案

我不知道该政策是对张贴的亵渎,但这里的问题:

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

enctype="mulitipart/form-data"

额外的 I 在那里停止从上传的文件。只好跑小提琴手地看到,这是从来没有发送文件摆在首位。

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

它应该阅读:

enctype="multipart/form-data"

这篇关于文件上传与ASP.Net MVC - 得到的名字,但没有文件流,我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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