ASP.NET Web API 上传文件
HTML表单:
<form id="form1" method="post"
enctype="multipart/form-data" action="http://localhost:2317/api/uploadFile">
<div>
<label for="Name">Name</label>
</div>
<div>
<input name="Name" type="text" />
</div>
<div>
<label for="Price">Price</label>
</div>
<div>
<input name="Price" type="text" />
</div>
<div>
<input name="ImageFile" type="file" id="me"/>
</div>
<div>
<input type="submit" name="name" value="submit" />
</div>
</form>
API Controller:
[Route("api/uploadFile")]
public HttpResponseMessage PostFile()
{
HttpResponseMessage result = null;
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > )
{
var docfiles = new List<string>();
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName);
postedFile.SaveAs(filePath);
docfiles.Add(filePath);
}
result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
}
else
{
result = Request.CreateResponse(HttpStatusCode.BadRequest);
}
return result;
}
HttpClient客户端上传:
//--------上传------------
HttpClient client = new HttpClient();
#region
MultipartFormDataContent form = new MultipartFormDataContent();
StreamContent fileContent = new StreamContent(File.OpenRead(@"D:\uni-all-resources\CloudShop\Source\Development\Uni2uni.iShop\Uni2uni.iShop.WebApi\UploadAudio\ios_2.pcm"));
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
fileContent.Headers.ContentDisposition.FileName = "ios_2.pcm";
form.Add(fileContent);
#endregion HttpResponseMessage res = client.PostAsync("http://app.uni2uni.com/api/Audio/UploadAudioResponseMessage", form).Result;
var uploadModel = res.Content.ReadAsStringAsync().Result;
ASP.NET Web API 上传文件的更多相关文章
- 前端AngularJS后端ASP.NET Web API上传文件
本篇体验使用AngularJS向后端ASP.NET API控制器上传文件. 首先服务端: public class FilesController : ApiController { //usi ...
- 返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, .net 4.5 带来的更方便的异步操作
原文:返璞归真 asp.net mvc (11) - asp.net mvc 4.0 新特性之自宿主 Web API, 在 WebForm 中提供 Web API, 通过 Web API 上传文件, ...
- 演示如何通过 web api 上传文件MVC40
演示如何通过 web api 上传文件WebApiWebFormHost/UploadFileController.cs /* * 通过 web api 上传文件 */ using System; u ...
- C# Web Api 上传文件
一. 使用默认方法上传文件: 1.Action: /// <summary> /// 上传文件 使用上传后的默认文件名称 /// 默认名称是BodyPart_XXXXXX,BodyPart ...
- 1.5 WEB API 上传文件
1.前提,设置跨域 2.在控制器头添加允许跨域 /// <summary> /// 文件管理口控制器 /// </summary> [EnableCors("*&qu ...
- HttpClient 测试web API上传文件实例
1.使用HttpClient 测试上传文件并且设置header信息: using Lemon.Common; using Newtonsoft.Json; using System; using Sy ...
- 数据採集之Web端上传文件到Hadoop HDFS
前言 近期在公司接到一个任务.是关于数据採集方面的. 需求主要有3个: 通过web端上传文件到HDFS; 通过日志採集的方式导入到HDFS; 将数据库DB的表数据导入到HDFS. 正好近期都有在这方面 ...
- asp.net限制了上传文件大小为..M,解决方法
asp.net限制了上传文件大小为4M,在:在web.config里加下面一句,加在<System.web></System.web>之间如下:<system.web&g ...
- php 下 html5 XHR2 + FormData + File API 上传文件
FormData的作用: FormData对象可以帮助我们自动的打包表单数据,通过XMLHttpRequest的send()方法来提交表单.当然FormData也可以动态的append数据.FormD ...
随机推荐
- C++关键字:mutable(转)
参考:http://blog.csdn.net/hxz_qlh/article/details/14475573 修饰成员变量,在const成员函数中可修改它,在C++中还从未用过这个关键字.
- BZOJ 4455: [Zjoi2016]小星星
Sol 容斥原理+树形DP. 这道题用的容斥思想非常妙啊!主要的思路就是让所有点与S集合中的点对应,可以重复对应,并且可以不用对应完全(意思是是S的子集也可以).这样他有未对应完全的,那就减去,从全都 ...
- MySQL重复数据
delete from porn where Id not in (select minid from (select min(id) as minid from porn group by view ...
- django的跨站请求访问
一.简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对于django中设置防跨站请求伪造功 ...
- 17.4---返回max,不用if
思路:借助max公式就可以了.max(x,y)=0.5*(x+y+|x-y|) 注意:1,结尾要加(int). 答案: max(x,y)=0.5*(x+y+|x-y|)
- C# XML和实体类之间相互转换(序列化和反序列化)
我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...
- 善用VS中的Code Snippet来提高开发效率
http://www.cnblogs.com/anderslly/archive/2009/02/16/vs2008-code-snippets.html http://www.cnblogs.com ...
- ubuntu hash sum mismatch error
$ sudo rm -rf /var/lib/apt/lists/* $ sudo apt-get update from: askubuntu.com
- mybatis xml 大于 小于
当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...
- zmqsend for FFmpeg
编译 FFmpeg 时,配置中添加 --enable-libzmq 就可以通过 tcp 通讯给 FFmpeg 发送命令 Usage: zmqsend [-h ip:port] TARGET COMMA ...