Пример #1
0
 public dynamic Upload([FromUri] UploadFileDto model)
 {
     return(UploadAsync(model).Result);
 }
Пример #2
0
 public async Task <dynamic> UploadAsync([FromUri] UploadFileDto model)
 {
     return(await UploadAsync(Request, model));
 }
Пример #3
0
        private async Task <dynamic> UploadAsync(HttpRequestMessage request, [FromUri] UploadFileDto model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            try
            {
                var mr          = new MessageResult();
                var fileUrls    = new List <string>();
                var httpRequest = HttpContext.Current.Request;
                foreach (string httpFile in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[httpFile];
                    if (postedFile != null && postedFile.ContentLength > 0)
                    {
                        var    fileName  = postedFile.FileName;
                        string extension = GetFileExtension(fileName);
                        bool   isAllowed = CheckAllowedExtensions(extension);
                        if (!isAllowed)
                        {
                            mr.Message = "不支持的文件格式:" + extension;
                            mr.Success = false;
                            return(await Task.FromResult(mr));
                        }

                        int maxM             = 10;
                        int MaxContentLength = 1024 * 1024 * maxM; //Size = 10 MB   //todo
                        if (postedFile.ContentLength > MaxContentLength)
                        {
                            mr.Message = string.Format("超出上传最大限制:{0}M", maxM);
                            mr.Success = false;
                            return(await Task.FromResult(mr));
                        }

                        var server     = HttpContext.Current.Server;
                        var saveFolder = server.MapPath(model.VirtualFolder.TrimEnd('/'));

                        var theFileName = fileName;
                        if (!string.IsNullOrWhiteSpace(model.FileName))
                        {
                            theFileName = model.FileName;
                        }
                        var savePath = Path.Combine(saveFolder, theFileName);
                        //Deletion exists file
                        if (File.Exists(savePath))
                        {
                            File.Delete(savePath);
                        }
                        MakeSureFolderExist(saveFolder);

                        postedFile.SaveAs(savePath);
                        var fileUrl = string.Format("{0}/{1}", model.VirtualFolder.TrimEnd('/'), theFileName);
                        fileUrls.Add(fileUrl);
                    }
                    else
                    {
                        fileUrls.Add(null);
                    }
                }
                mr.Message = "上传完成";
                mr.Success = true;
                mr.Data    = fileUrls;
                return(await Task.FromResult(mr));
            }
            catch (Exception ex)
            {
                return(request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }