HTML5技术

WebApi2 文件图片上传下载 - 柒小栈主(2)

字号+ 作者:H5之家 来源:H5之家 2017-05-27 16:02 我要评论( )

CreateDirectoryIfNotExist( string filePath) 3 { 4 if (! Directory.Exists(filePath)) 5 { 6 Directory.CreateDirectory(filePath); 7 } ; 9 } 2.文件上传下载接口和图片大同小异。 1 using QX_Frame.App.WebApi

CreateDirectoryIfNotExist(string filePath) 3 { 4 if (!Directory.Exists(filePath)) 5 { 6 Directory.CreateDirectory(filePath); 7 } ; 9 }

2.文件上传下载接口和图片大同小异。

1 using QX_Frame.App.WebApi; 2 using QX_Frame.FilesCenter.Helper; 3 using QX_Frame.Helper_DG; 4 using System.Collections.Generic; 5 using System.Diagnostics; 6 using System.IO; 7 using System.Linq; 8 using System.Net; 9 using System.Net.Http; 10 using System.Net.Http.Headers; 11 using System.Text; 12 using System.Threading.Tasks; 13 using System.Web; 14 using System.Web.Http; * 17 * author:qixiao 18 * create:2017-5-26 16:54:46 QX_Frame.FilesCenter.Controllers 21 { FilesController : WebApiControllerBase 23 { HttpResponseMessage Get(string fileName) 26 { 27 HttpResponseMessage result = null; ); 30 FileInfo foundFileInfo = directoryInfo.GetFiles().Where(x => x.Name == fileName).FirstOrDefault(); 31 if (foundFileInfo != null) 32 { 33 FileStream fs = new FileStream(foundFileInfo.FullName, FileMode.Open); 34 35 result = new HttpResponseMessage(HttpStatusCode.OK); 36 result.Content = new StreamContent(fs); ); ); 39 result.Content.Headers.ContentDisposition.FileName = foundFileInfo.Name; 40 } { 43 result = new HttpResponseMessage(HttpStatusCode.NotFound); 44 } result; 47 } Task<IHttpActionResult> Post() 51 { root = IO_Helper_DG.RootPath_MVC; newRoot = root + ; IO_Helper_DG.CreateDirectoryIfNotExist(newRoot); 59 60 List<string> fileNameList = new List<string>(); 61 62 StringBuilder sb = new StringBuilder(); fileTotalSize = 0; fileIndex = 1; HttpFileCollection files = HttpContext.Current.Request.Files; Task.Run(() => 72 { 73 foreach (var f in files.AllKeys) 74 { 75 HttpPostedFile file = files[f]; 76 if (!string.IsNullOrEmpty(file.FileName)) 77 { fileLocalFullName = newRoot + file.FileName; 80 81 file.SaveAs(fileLocalFullName); ); 84 85 FileInfo fileInfo = new FileInfo(fileLocalFullName); 86 87 fileTotalSize += fileInfo.Length; ); 90 91 fileIndex++; + fileLocalFullName); 94 } 95 } 96 }); Json(Return_Helper.Success_Msg_Data_DCount_HttpCode($, fileNameList, fileNameList.Count)); 100 } 101 } 102 }

实现了上述两个控制器代码以后,我们需要前端代码来调试对接,代码如下所示。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • DIV 行内关联 box-shadow对象盒子阴影以及图片阴影 - yueyang2017

    DIV 行内关联 box-shadow对象盒子阴影以及图片阴影 - yueyang2017

    2017-05-22 10:00

  • Spring+SpringMVC+MyBatis深入学习及搭建(三)——MyBatis全局配置文件解析 - Joanna

    Spring+SpringMVC+MyBatis深入学习及搭建(三)——MyBatis全局配置文

    2017-05-19 14:01

  • Nodejs 传图片的两种方式 - 菜鸟的进击

    Nodejs 传图片的两种方式 - 菜鸟的进击

    2017-05-13 10:03

  • C# 创建压缩文件 - sparkdev

    C# 创建压缩文件 - sparkdev

    2017-05-09 14:03

网友点评
a