Asp.Net Framework webapi2 文件上传与下载 前端界面采用Ajax的方式执行
一、项目结构
1.App_Start配置了跨域访问,以免请求时候因跨域问题不能提交。具体的跨域配置方式如下,了解的朋友请自行略过。
跨域配置:NewGet安装dll Microsofg.AspNet.Cors
然后在App_Start 文件夹下的WebApiConfig.cs中写入跨域配置代码。
WebApiConfig 2 { Register(HttpConfiguration config) 4 { 5 // Web API configuration and services config.MapHttpAttributeRoutes(); Web API configuration and services config.EnableCors(, , )); 13 14 config.Routes.MapHttpRoute( , , 17 defaults: new { id = RouteParameter.Optional } 18 ); //config.Filters.Add(new App.WebApi.Filters.ExceptionAttribute_DG()); 22 } 23 }
跨域就算完成了,请自行测试。
2.新建两个控制器,一个PicturesController.cs,一个FilesController.cs当然图片也是文件,这里图片和文件以不同的方式处理的,因为图片的方式文件上传没有成功,所以另寻他路,如果在座的有更好的方式,请不吝赐教!
二、项目代码
1.我们先说图片上传、下载控制器接口,这里其实没什么好说的,就一个Get获取文件,参数是文件全名;Post上传文件;直接上代码。
1 using QX_Frame.App.WebApi; 2 using QX_Frame.FilesCenter.Helper; 3 using QX_Frame.Helper_DG; 4 using QX_Frame.Helper_DG.Extends; 5 using System; 6 using System.Collections.Generic; 7 using System.Diagnostics; 8 using System.IO; 9 using System.Linq; 10 using System.Net; 11 using System.Net.Http; 12 using System.Net.Http.Headers; 13 using System.Text; 14 using System.Threading.Tasks; 15 using System.Web.Http; * 18 * author:qixiao 19 * create:2017-5-26 16:54:46 QX_Frame.FilesCenter.Controllers 22 { PicturesController : WebApiControllerBase 24 { HttpResponseMessage Get(string fileName) 27 { 28 HttpResponseMessage result = null; ); 31 FileInfo foundFileInfo = directoryInfo.GetFiles().Where(x => x.Name == fileName).FirstOrDefault(); 32 if (foundFileInfo != null) 33 { 34 FileStream fs = new FileStream(foundFileInfo.FullName, FileMode.Open); 35 36 result = new HttpResponseMessage(HttpStatusCode.OK); 37 result.Content = new StreamContent(fs); ); ); 40 result.Content.Headers.ContentDisposition.FileName = foundFileInfo.Name; 41 } { 44 result = new HttpResponseMessage(HttpStatusCode.NotFound); 45 } result; 48 } Task<IHttpActionResult> Post() 53 { 54 if (!Request.Content.IsMimeMultipartContent()) 55 { Exception_DG(, 2005); 57 } 58 string root = IO_Helper_DG.RootPath_MVC; ); provider = ); Request.Content.ReadAsMultipartAsync(provider); 66 67 List<string> fileNameList = new List<string>(); 68 69 StringBuilder sb = new StringBuilder(); fileTotalSize = 0; fileIndex = 1; (MultipartFileData file in provider.FileData) 77 { newRoot = root + ; 80 81 IO_Helper_DG.CreateDirectoryIfNotExist(newRoot); (File.Exists(file.LocalFileName)) 84 { fileName = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2); newFileName = Guid.NewGuid() + + fileName.Split()[1]; newFullFileName = newRoot + + newFileName; ); 93 94 FileInfo fileInfo = new FileInfo(file.LocalFileName); 95 96 fileTotalSize += fileInfo.Length; ); 99 100 fileIndex++; 101 102 File.Move(file.LocalFileName, newFullFileName); + newFullFileName); 105 } 106 } Json(Return_Helper.Success_Msg_Data_DCount_HttpCode($, fileNameList, fileNameList.Count)); 109 } 110 } 111 }
里面可能有部分代码在Helper帮助类里面写的,其实也仅仅是获取服务器根路径和如果判断文件夹不存在则创建目录,这两个代码的实现如下:
RootPath_MVC 2 { ); } 4 }