Web Api 接收图片
public async Task<HttpResponseMessage> Upload() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string fileSaveLocation = HttpContext.Current.Server.MapPath("/img"); CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation); List<string> files = new List<string>(); try { await Request.Content.ReadAsMultipartAsync(provider); ].LocalFileName; FileAttributes attr = File.GetAttributes(path); if (attr == FileAttributes.Directory) { Directory.Delete(path, true); } else { File.Delete(path); } foreach (var file in provider.FileData) { files.Add(file.LocalFileName); } return new HttpResponseMessage(HttpStatusCode.OK) { Content = ]) }; } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex); } }
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider { public CustomMultipartFormDataStreamProvider(string path) : base(path) { } public override string GetLocalFileName(HttpContentHeaders headers) { return headers.ContentDisposition.FileName.Replace("\"", string.Empty); } }
function upload() { var url = "http://localhost:42561/api/upload"; var formData = new FormData($("#uploadForm")[0]); $.ajax({ url: url, type: "post", data: formData, async: false,//设置所有请求为同步请求 cache: false,//false 表示第一次请求完毕之后,如果再次去请求,直接从缓存里面读取而不是再到服务器端读取。该值默认为true contentType: false, processData: false,//false 表示提交的时候不会序列化 data,而是直接使用 data,默认为 true success: function (result) { $("img").attr("src", result); console.log($("img").length); }, error: function (result) { console.log(result); } }); return false; }
<div> <form id="uploadForm"> <input type="file" name="file" value="" /><br /> </form> <input type="button" value="上传" onclick="upload();" /> </div> <hr /> <div> <img src="" /> </div>
Web Api 接收图片的更多相关文章
- Windows Azure 系列-- 使用Azure + Web API实现图片上传
1. 创建1个Azure账号,登录之后创建1个AzureStorage.左下方点Manage Access会看到Primary Access Key和Storage Account,记住它们的位置,等 ...
- HttpClient 同时上传多个文件及参数, 同时利用 Web Api 接收
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) { client.BaseAddress = ...
- C# WEB.API 接收并解析保存base64格式的图片
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...
- asp.net web api实现图片点击式图片验证码
现在验证码的形式越来越丰富,今天要实现的是在点击图片中的文字来进行校验的验证码,如图 这种验证码验证是验证鼠标是否选中了图片中文字的位置,以及选择的顺序,产生验证码的时候可以提供一组底图,然后随机获取 ...
- asp.net web api返回图片至前端
var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new ByteArrayContent(da ...
- 文件上传之Html5 + jQuery上传、asp.net web api接收
HTML: <div> <label for="fileUpload"> 选择文件 </label> <br/> <input ...
- ASP.NET Core Web API接收文件传输
ASP.NET解析API参数的方式有很多种,包括[FromBody],[FromForm],[FromServices],[FromHeader][FromQuery]. 文件传输方式也分很多种,包括 ...
- Wince 6.0适用 .NET 使用HttpRequest的Post上传文件,服务端的Web API接收Post上传上来的文件 代码
//调用的示例 private string fileName = "InStorageData.csv"; string filePath = parentPath + Comm ...
- Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...
随机推荐
- enable_shared_from_this类的作用和实现
使用举例 实际中, 经常需要在一个被shared_ptr管理的对象的内部获取自己的shared_ptr. 比如: 通过this指针来构造一个shared_ptr, 如下: struct Bad { v ...
- imageview无法显示图片:java.lang.RuntimeException: Canvas: trying to draw too large(281520000bytes) bitmap
图片太大需要压缩. 压缩方法:http://jingyan.baidu.com/article/cdddd41c3ef41153ca00e162.html 如果特别大(几十M),可以先用在线的图片压缩 ...
- 34.Ajax
优先级 如果发送的是[普通数据] jQuery XMLHttpRequest iframe 如果发送的是[文件] iframe jQuery(FormData) XMLHttpRequest(Form ...
- conda创建py27虚拟环境安装theano(anaconda3)
现在python3已经成为主流的python环境,大部分的package都兼容python3,仍然有一小部分,或者说是某一领域的package需要使用python2.本人现在主要在利用python做机 ...
- PAT乙级-1056. 组合数的和(15)
给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入 ...
- C和C#的区别
c:面向过程,语法太麻烦,但对硬件的底层编程和对内存的管理的灵活性方面c是其他高级语言所不可及的. c#:纯面向对象的(跟java很像如果你对java了解估计你就会明白c&c#之间的区别了), ...
- C# 值类型与引用类型的异同
int,bool,decimal等为值类型 List,Stream等为引用类型 用等号设置一个值类型变量等于另一个变量时,会完成复制,之后这两个变量相互之间没有任何影响: 对引用使用等号时,这两个引用 ...
- [.Net Core] 在 Mvc 中简单使用日志组件
在 Mvc 中简单使用日志组件 基于 .Net Core 2.0,本文只是蜻蜓点水,并非深入浅出. 目录 使用内置的日志组件 简单过渡到第三方组件 - NLog 使用内置的日志 下面使用控制器 Hom ...
- 《深入理解计算机系统》第7章:重定位PC相对引用的理解
在第七章<链接>中的静态链接有对符号进行重定位PC相对引用的处理,书上对应的还有公式,但不是很好理解.现做实验对公式进行理解(公式内容如有兴趣可以参考原文)
- poj-1207 THE 3n+1 problem
Description Problems in Computer Science are often classified as belonging to a certain class of pro ...