博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webapi文件上传和下载
阅读量:4673 次
发布时间:2019-06-09

本文共 4315 字,大约阅读时间需要 14 分钟。

文件上传我们可以做上传文件保存到图片与导入数据,下载可以下载多样的文件。

上传:实例是单个文件导入

NopiHelper:

①简单的上传图片进行保存,方法跟MVC中的一样

[HttpPost]        public async Task
PostFormData2() { IList
paths = new List
(); var files = HttpContext.Current.Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFile file = files[0]; string filePath = $@"upload/{DateTime.Now.ToString("yyyyMMddssfff")}_{file.FileName}"; DirectoryInfo rootDir2 = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory); string path = System.IO.Path.Combine( rootDir2.FullName, filePath); var dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } file.SaveAs(path); paths.Add(path); } return await Task.FromResult(Ok(new { errcode = 0, data = paths })); }

 

导入数据:这里需要我们Nuget:Npoi

[HttpPost]        public async Task
UploadFile() { try { //得到上传的文件 HttpPostedFile file = HttpContext.Current.Request.Files[0];           //转DataTable DataTable dt = NopiHelper.ExcelToTable(file.InputStream, 0, 0, Path.GetExtension(file.FileName));           //通过DataTable转实体 List
list = NopiHelper.Mapper
(dt); foreach (TVideosurveillance item in list) { TVideosurveillance model = await dbOracle.TVideosurveillances.AsQueryable().FirstAsync(x => x.Dwname == item.Dwname); if (model == null) { model.Dwname = item.Dwname; model.Dwcode = item.Dwcode; model.Code = item.Code; model.Ip = item.Ip; model.Sbtdmc = item.Sbtdmc; double[] vs = WGS84Helper.bd09togcj02(item.Coordx.ToDouble(), item.Coordy.ToDouble()); double[] vs2 = WGS84Helper.gcj02towgs84(vs[0], vs[1]); model.Coordx = vs2[0] + ""; model.Coordy = vs2[1] + ""; dbOracle.TVideosurveillances.Insert(model); } } return await Task.FromResult(Ok(new { errcode = 0, data = "成功" })); } catch (Exception ex) { return await Task.FromResult(Ok(new { errcode = 0, data = ex.Message })); } }

 

 

下载(导出):webapi中需要我们对  HttpResponseMessage 进行设置

///         /// 下载只能使用Get方式,需要我们返回 HttpResponseMessage        ///         /// 
[HttpGet] public async Task
Test() { DataTable dt = NopiHelper.Mapper
(); //导出的名字,需要带扩展名 *.xls *.xlsx string fileName = "测试.xls"; Stream stream = NopiHelper.StreamFromDataTable(dt, fileName); //设置状态 HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK); //内容 httpResponseMessage.Content = new StreamContent(stream); //类型 httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel"); //响应内容的值,附件形式 httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = HttpUtility.UrlEncode(Path.GetFileName(fileName)) }; return await Task.FromResult(httpResponseMessage); }

 

MS Excel具有以下观察到的MIME类型:

  • application/vnd.ms-excel (官方)
  • application/msexcel
  • application/x-msexcel
  • application/x-ms-excel
  • application/x-excel
  • application/x-dos_ms_excel
  • application/xls
  • application/x-xls
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (XLSX)

 

转载于:https://www.cnblogs.com/Sea1ee/p/10478290.html

你可能感兴趣的文章
探索从 MVC 到 MVVM + Flux 架构模式的转变
查看>>
tornado的异步效果
查看>>
*2.3.2_加入env
查看>>
JS中SetTimeOut和SetInterval方法的区别?
查看>>
Protocol Buffers proto语言语法说明
查看>>
Hibernate双向一对一对象关系模型映射
查看>>
正则表达式(下)
查看>>
熟悉常用的HDFS操作
查看>>
Linux自动化运维第十八课
查看>>
web 10
查看>>
jquery--动态篇
查看>>
npm 是干什么的
查看>>
Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
查看>>
查找路径php.ini文件到底在哪里?
查看>>
传统认知PK网络认知 刚子扯谈烤串认知
查看>>
字节数组java加密与解密
查看>>
矩形运算
查看>>
php 备份mysql数据库(joomla数据库可直接使用,其他数据库稍作修改即可)
查看>>
使用HttpSessionListener接口监听Session的创建和失效
查看>>
Windows Phone XNAでアニメーション - ぐるぐる
查看>>