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 接收图片的更多相关文章

  1. Windows Azure 系列-- 使用Azure + Web API实现图片上传

    1. 创建1个Azure账号,登录之后创建1个AzureStorage.左下方点Manage Access会看到Primary Access Key和Storage Account,记住它们的位置,等 ...

  2. HttpClient 同时上传多个文件及参数, 同时利用 Web Api 接收

    using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) { client.BaseAddress = ...

  3. C# WEB.API 接收并解析保存base64格式的图片

    using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...

  4. asp.net web api实现图片点击式图片验证码

    现在验证码的形式越来越丰富,今天要实现的是在点击图片中的文字来进行校验的验证码,如图 这种验证码验证是验证鼠标是否选中了图片中文字的位置,以及选择的顺序,产生验证码的时候可以提供一组底图,然后随机获取 ...

  5. asp.net web api返回图片至前端

    var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new ByteArrayContent(da ...

  6. 文件上传之Html5 + jQuery上传、asp.net web api接收

    HTML: <div> <label for="fileUpload"> 选择文件 </label> <br/> <input ...

  7. ASP.NET Core Web API接收文件传输

    ASP.NET解析API参数的方式有很多种,包括[FromBody],[FromForm],[FromServices],[FromHeader][FromQuery]. 文件传输方式也分很多种,包括 ...

  8. Wince 6.0适用 .NET 使用HttpRequest的Post上传文件,服务端的Web API接收Post上传上来的文件 代码

    //调用的示例 private string fileName = "InStorageData.csv"; string filePath = parentPath + Comm ...

  9. Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程

    Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...

随机推荐

  1. 使用Ant打包Java后台程序

    概述 本文通过一个简单的Java Application例子描述如何用ANT完成基本的程序打包工作.包含文件拷贝.编译.打包三部分:完成这三部就可以得到一个可运行的程序包了. ANT的安装,环境变量的 ...

  2. php 后端跨域请求

    header("Access-Control-Allow-Origin: http://a.com"); // 允许a.com发起的跨域请求 //如果需要设置允许所有域名发起的跨域 ...

  3. 关于better-scroll中的问题点

    最近在学习vue和better-scroll结合开发音乐APP,看着视频介绍中在制作slidet轮播图的时候,视频中讲解要想实现无缝滚动,则加入snapLoot:true,但是不可以单独引入它,必须将 ...

  4. vue零基础学习--搭建项目

    一.script引入(联系使用,小型项目) 直接下载并用 <script> 标签引入,Vue 会被注册为一个全局变量. <script src="https://cdn.j ...

  5. IPFS: BitSwap协议(数据块交换)

    原创 2018-01-11 飞向未来 IPFS指南 BitSwap协议 IPFS节点之间是如何进行数据交换的?本文来讲一下这个问题. IPFS在BitTorrent的基础上实现了p2p数据交换协议:B ...

  6. load vs. initialize

    这篇文章来对比一下NSObject类的两个方法,+load与+initialize. + (void)load; Invoked whenever a class or category is add ...

  7. 关于Oracle-SQL语句性能优化

    Oracle-Sql语句性能优化 相信许多从事几年的开发人员都有过一些经验,相对于刚出来的毕业生而言,对于同种操作sql结果,他们的代码性能会更高一些.虽然本人还是个实习生,在这还是写写自己     ...

  8. Java 面试宝典-2017

    http://www.cnblogs.com/nelson-hu/p/7190163.html Java面试宝典-2017   Java面试宝典2017版 一. Java基础部分........... ...

  9. 001-List,数组,Set,Map属性的映射

    hibernate.cfg.xml: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configur ...

  10. Spring Boot Junit单元测试

    http://blog.csdn.net/catoop/article/details/50752964