架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 16295|回复: 1

[ASP.NET] asp.net mvc上传接口demo

[复制链接]
发表于 2016-8-17 17:17:22 | 显示全部楼层 |阅读模式
QQ图片20160817171313.png

文件上传或者图片上传也好,都是把文件流上传到服务器,

至于怎么接收,mvc其实都已经封装好了,就是Request。Files接收。

demo代码如下:

  1. [HttpPost]
  2.         public ActionResult IndexPost()
  3.         {
  4.             try
  5.             {
  6.                 Stream stream = Request.Files["file"].InputStream;
  7.                 string imgPath = AppDomain.CurrentDomain.BaseDirectory + @"" + Request.Files["file"].FileName;
  8.                 using (FileStream fs = new FileStream(imgPath, FileMode.Create))
  9.                 {
  10.                     byte[] bytes = new byte[stream.Length];
  11.                     int numBytesRead = 0;
  12.                     int numBytesToRead = (int)stream.Length;
  13.                     stream.Position = 0;
  14.                     while (numBytesToRead > 0)
  15.                     {
  16.                         int n = stream.Read(bytes, numBytesRead, Math.Min(numBytesToRead, int.MaxValue));
  17.                         if (n <= 0)
  18.                         {
  19.                             break;
  20.                         }
  21.                         fs.Write(bytes, numBytesRead, n);
  22.                         numBytesRead += n;
  23.                         numBytesToRead -= n;
  24.                     }
  25.                     fs.Close();
  26.                 }
  27.                 return Json(new { success = true });
  28.             }
  29.             catch (Exception ex)
  30.             {
  31.                 return Json(new { message = ex.Message });
  32.             }
  33.         }
复制代码


我自己已经测试成功,请大家自行测试。




上一篇:CMD获取笔记本连过的所有wifi密码
下一篇:.net Memcached 使用 及.NET客户端调用
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2016-8-17 17:23:49 | 显示全部楼层
  1. public JsonResult File()
  2.         {
  3.             try
  4.             {
  5.                 DirectoryInfo dir = System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\File");
  6.                 Stream files = Request.Files[0].InputStream;
  7.                 string path = dir.FullName + @"" + Request.Files[0].FileName;
  8.                 using (FileStream fs = new FileStream(path,FileMode.Append,FileAccess.Write,FileShare.ReadWrite))
  9.                 {
  10.                     byte[] buffer = new byte[512];
  11.                     long contentLength = files.Length;
  12.                     int length = files.Read(buffer, 0, buffer.Length);
  13.                     while (length > 0)
  14.                     {
  15.                         fs.Write(buffer, 0, length);
  16.                         buffer = new byte[512];
  17.                         length = files.Read(buffer, 0, buffer.Length);
  18.                     }
  19.                     fs.Flush();
  20.                     fs.Close();
  21.                 }
  22.                 return Json(new { success = true, message = path });
  23.             }
  24.             catch (Exception ex)
  25.             {
  26.                 return Json(new { success = false, message = "error" });
  27.             }

  28.             
  29.             
  30.             
  31.         }
复制代码



这个是自己以前写的接收大文件上传的接口,自己测试上传1g文件,没有问题。
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-9-1 21:12

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表