c# - 上传图像并获取其字节

标签 c# .net 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>

我应该如何获取 Controller 中的图像字节?

最佳答案

将以下内容添加到您的 Controller 方法中。

      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
      }

关于c# - 上传图像并获取其字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1934289/

相关文章:

c# - 在按键时激活未聚焦的 C# 程序(例如 F1 将标签设置为 "Hello World")

c# - 为什么重写的 ToString() 在项目添加到 ComboBox 时不返回我想要的内容?

c# - 从 NameValueCollection 中删除单个值

c# - COM 技术的当前模拟是什么?

.net - ADO 与数据集和数据表

sql - 获得简单的成员(member)资格以使用 azure 网站

asp.net-mvc - 过滤 Kendo UI MVC 网格时构建自定义谓词

c# - 设置一个方法来返回作为参数传递的函数的返回值

.net - 使用 ASP.NET 和全局化获取 WCF 服务中的浏览器文化

asp.net-mvc - 如何在 asp.net mvc 中使用 html helper 创建只读文本?