上传图像并获取其字节

Cof*_*ode 1 .net c# asp.net-mvc file-upload

我有这样的表格

<form name="" method="post" action="Save"  enctype="multipart/form-data">
         <div id="dialog" title="Upload files">        
         <input type="file" id="Image" name="fileUpload" size="23"/>
         </div>
         <input  type="submit" value="Create" />
 </form>
Run Code Online (Sandbox Code Playgroud)

我怎么能得到控制器中的图像字节?

wom*_*omp 11

将以下内容添加到控制器方法中.

      var file = Request.Files["Image"];
      if ( file != null )
      {
         byte[] fileBytes = new byte[file.ContentLength];
         file.InputStream.Read( fileBytes, 0, file.ContentLength );

         // ... now fileBytes[] is filled with the contents of the file.
      }
      else
      {
         // ... error handling here
      }
Run Code Online (Sandbox Code Playgroud)

  • 它应该是var file = Request.Files ["uploadFile"]; 它将元素名称作为索引器:) (2认同)