花猫阳
花猫阳
  • 发布:2015-05-07 11:05
  • 更新:2021-02-09 10:19
  • 阅读:13161

web API图片上传

分类:HTML5+

最近研究了下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;  
        }

这个也是在网上找的资料,在此基础上添加了修改文件名功能,图片会保存在userimage文件夹下的子目录(如没有userimage就手动创建个),子目录的名称为客户端task.addData("","")添加的内容,文件名为客户端生成的随机数.

0 关注 分享

要回复文章请先登录注册

1***@qq.com

1***@qq.com

有问题可以找我,一起学习吧

我的个人项目已经上线了,我使用的是七牛云存储(阿里也不错的),大概原理也一样吧,你们可以看看,很多功能都有了。

1、Android的(软著没申请下来,暂时上不了架):http://d.firim.top/tc7b?utm_source=fir&utm_medium=qrhttp://d.firim.top/tc7b?utm_source=fir&utm_medium=qr

2、iOS已经上架搜索:猫云—优质贴心的宠物平台

3、顺便也给你们给链接吧,例如iOS做唤起第三方或某个APP时可以使用到 https://apps.apple.com/cn/app/猫云-优质贴心的宠物社交平台/id1474103355
2021-02-09 10:19
3***@qq.com

3***@qq.com

前端呢
2019-07-24 10:18
花猫阳

花猫阳 (作者)

这样写有个bug,上传多张图片时,共用一个随机数导致文件已存在出错,所以随机数那块改成系统时间+随机数吧
2015-05-07 14:03