WepAPI下载与上传文件
来源:互联网 发布:网络有什么坏处 编辑:程序博客网 时间:2024/06/05 06:13
1、上传文件,代码是没问题的,但是我在调用的时候出现了iframe跨域问题,(没有解决),在不牵扯主页面套用子页面的情况下使用是可以的。
代码:
#region 上传文件
[HttpPost]
public HttpResponseMessage UpLoadFile()
{
// 检查是否是 multipart/form-data
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string json;
string productName = HttpContext.Current.Request.Form["TempProductName"];
HttpPostedFile file = HttpContext.Current.Request.Files["TempLicense"];
if (file != null)
{
if (file.ContentLength / 1024 > 100)//不能超过100KB
{
json = "{\"success\":0,\"msg\":\"The file is too large\"}";
}
else
{
var fileSavePath = HttpContext.Current.Server.MapPath("/") + "TempLicenseFile/" + productName + "/";
if (Directory.Exists(fileSavePath))
{
Directory.Delete(fileSavePath, true);
}
Directory.CreateDirectory(fileSavePath);
String filePath = fileSavePath + file.FileName;
file.SaveAs(filePath);
json = "{\"success\":1,\"msg\":\"success\"}";
}
}
else
{
json = "{\"success\":0,\"msg\":\"no file\"}";
}
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
#endregion
2、下载文件。当时是给安卓那边调用。
代码:
/// <summary>
/// 下载文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="name">客户端保存的文件名</param>
/// <returns></returns>
public HttpResponseMessage DownLoad(string filePath, string name)
{
string customFileName = name;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentDisposition.FileName = customFileName;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentLength = new FileInfo(filePath).Length;
return response;
}
- WepAPI下载与上传文件
- 文件上传与下载----上传
- 文件上传与下载
- 文件上传与下载
- 上传与下载文件
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件上传与下载
- 文件下载与上传
- 文件上传与下载
- cocos2dx-lua 3.x 热更新方法
- oracle中取当前时间和某时间字段的差值
- 像素大小的计算
- android 菜单如何显示图标.
- 基于JavaScript、Javabean、Servlet、ajax的异步请求登录注册找回密码Javaweb项目
- WepAPI下载与上传文件
- iOS开发系列---如何从菜鸟到专家
- 二叉树的遍历
- java继承
- Java—内部类(普通,静态,局部,匿名)
- Maven学习总结(一)——Maven入门
- gcj 2008 apac problem c
- Golang 语法 知识点提要
- 类。字段。方法。属性之间的定义及部分区别。