• WebAPI图片上传


    public Task<HttpResponseMessage> PostFormData()
            {
                // Check if the request contains multipart/form-data.
                // 检查该请求是否含有multipart/form-data
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }
    
                string root = HttpContext.Current.Server.MapPath("~/userImage");
                var provider = new MultipartFormDataStreamProvider(root);
    
                // Read the form data and return an async task.
                // 读取表单数据,并返回一个async任务
                var task = Request.Content.ReadAsMultipartAsync(provider).
                    ContinueWith<HttpResponseMessage>(t =>
                    {
                        if (t.IsFaulted || t.IsCanceled)
                        {
                            Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
                        }
    
                        // This illustrates how to get the file names.
                        // 以下描述了如何获取文件名
                        foreach (MultipartFileData file in provider.FileData)
                        {
                            //新文件夹路径
                            string newRoot = root + "\" + provider.FormData.GetValues(1)[0].ToString();
                            if (!Directory.Exists(newRoot))
                            {
                                Directory.CreateDirectory(newRoot);
                            }
                            Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                            Trace.WriteLine("Server file path: " + file.LocalFileName);
                            if (File.Exists(file.LocalFileName)) 
                            {
                                //原文件名称
                                string fileName = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2);
                                //新文件名称   随机数
                                string newFileName = provider.FormData.GetValues(0)[0] + "." + fileName.Split(new char[] { '.' })[1];
                                File.Move(file.LocalFileName, newRoot + "\" + newFileName);
                            }
                        }
                        return Request.CreateResponse(HttpStatusCode.OK);
                    });
    
                return task;
            }
     [HttpPost]
            public async Task<Dictionary<string, string>> Post(int id = 0)
            {
    
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }
                Dictionary<string, string> dic = new Dictionary<string, string>();
                string root = HttpContext.Current.Server.MapPath("~/App_Data");//指定要将文件存入的服务器物理位置
                var provider = new MultipartFormDataStreamProvider(root);
                try
                {
                    // Read the form data.
                    await Request.Content.ReadAsMultipartAsync(provider);
    
                    // This illustrates how to get the file names.
                    foreach (MultipartFileData file in provider.FileData)
                    {//接收文件
                        Trace.WriteLine(file.Headers.ContentDisposition.FileName);//获取上传文件实际的文件名
                        Trace.WriteLine("Server file path: " + file.LocalFileName);//获取上传文件在服务上默认的文件名
                    }//TODO:这样做直接就将文件存到了指定目录下,暂时不知道如何实现只接收文件数据流但并不保存至服务器的目录下,由开发自行指定如何存储,比如通过服务存到图片服务器
                    foreach (var key in provider.FormData.AllKeys)
                    {//接收FormData
                        dic.Add(key, provider.FormData[key]);
                    }
                }
                catch
                {
                    throw;
                }
                return dic;
            }

     原文地址:http://blog.csdn.net/starfd/article/details/45393089

  • 相关阅读:
    数据结构和算法之最大堆
    AStar
    合并SkinnedMeshRender实现换装
    Asp.net MVC 解决:CS0308: 非泛型 方法“System.Web.Mvc.Html.LinkExtensions.ActionLink
    使用jquery解析Json , 浏览器之间数据格式校验的细微差异.
    Js跳转代码代替RedirectToAction是否违背MVC
    情 人 节 快 乐
    UCenter 1.5.0 整合 Bug
    ping [主机名] 4
    关于VS2008调试时端口号不一致的问题
  • 原文地址:https://www.cnblogs.com/sunxuchu/p/5983611.html
Copyright © 2020-2023  润新知