CustomMultipartstreamprovider抛出了一个异常

本文关键字:一个 异常 CustomMultipartstreamprovider | 更新日期: 2023-09-27 18:05:12

我使用asp.net mvc 4作为服务。我需要能够从客户端应用程序接受张贴图像/文件。我写了一个代码,它工作得很好,但现在2到3周后它不工作显示一个异常

CustomMultipartstreamprovider抛出异常

我不知道是什么问题,需要帮助。

我的代码是

public class UploadController : ApiController
    {
        private static IExceptionLogs _iException = new ExceptionLogsBLO();
        private readonly static string className = "Upload";
        private static string methodName = string.Empty;
        public async Task<HttpResponseMessage> PostFile() 
        {
            methodName = "PostFile";
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            //string root = HttpContext.Current.Server.MapPath("~/App_Data");
            string root = HttpContext.Current.Server.MapPath("~/Files");
            var provider = new CustomMultipartFormDataStreamProvider(root);
            //var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith();
            try
            {
                StringBuilder sb = new StringBuilder(); // Holds the response body
                string senderID = null;
                string receiverId = null;
                string Exten = null;
                Int64 Type = 0;
                Int64 IsGroup = 0;
                // Read the form data and return an async task.
                await Request.Content.ReadAsMultipartAsync(provider);
                // This illustrates how to get the form data.
                foreach (var key in provider.FormData.AllKeys)
                {
                   // Trace.WriteLine(key.Headers.ContentDisposition.FileName);

                    foreach (var val in provider.FormData.GetValues(key))
                    {
                        sb.Append(string.Format("{0}: {1}'n", key, val));
                        if (key == "senderId") 
                        {
                            senderID = val;
                        }
                        else if(key == "receiverId")
                        {
                             receiverId = val;
                        }
                        else if(key == "exten")
                        {
                            Exten = val;
                        }
                        else if (key == "fileType") 
                        {
                            if (val == "image")
                            {
                                Type = 1;
                            }
                            else if (val == "audio")
                            {
                                Type = 2;
                            }
                            else if (val == "video")
                            {
                                Type = 4;
                            }
                            else 
                            {
                                Type = 3;
                            }
                        }
                        else if (key == "IsGroup")
                        {
                            IsGroup = Convert.ToInt64(val);
                        }
                    }
                }
                // This illustrates how to get the file names for uploaded files.
                foreach (var file in provider.FileData)
                {
                    FileInfo fileInfo = new FileInfo(file.LocalFileName);
                    sb.Append(string.Format("Uploaded file: {0} ({1} bytes)'n", fileInfo.Name, fileInfo.Length));
                    // file save in database code 
                    FileUploadModel fileData = new FileUploadModel();
                    fileData.FileName = fileInfo.Name;
                    fileData.FileType = Type;
                    fileData.SenderId = Convert.ToInt64(senderID);
                   // fileData.ReceiverId = Convert.ToInt64(receiverId);
                    fileData.ReceiverId = receiverId;
                    fileData.FileExten = Exten;
                    fileData.IsRead = Convert.ToBoolean(0);
                    fileData.date = DateTime.Now.ToUniversalTime();
                    fileData.IsGroup = IsGroup;
                    FileUploadBLO fileBlo = new FileUploadBLO();
                    DbResult result = fileBlo.FileUploadData(fileData);
                }
                return new HttpResponseMessage()
                {
                    Content = new StringContent(sb.ToString())
                };
            }
            catch(Exception ex)
            {
                ExceptionLogsModel error = new ExceptionLogsModel(ex.Message, ex.StackTrace.ToString(), DateTime.Now, className, methodName);
                _iException.InsertException(error);
            }
            return null;
        }
    }

 class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
    {
        public CustomMultipartFormDataStreamProvider(string path)
            : base(path)
        { }
        public override string GetLocalFileName(HttpContentHeaders headers)
        {
            return headers.ContentDisposition.FileName.Replace("'"", string.Empty);
           // var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName";
           // return name.Replace("'"", string.Empty); //this is here because Chrome submits files in quotation marks which get treated as part of the filename and get escaped
        }
    }

CustomMultipartstreamprovider抛出了一个异常

在我的案例中,文件上传的目标文件夹(保存文件的文件夹)与Web应用程序目录链中的虚拟目录不同,导致出现此异常。作为一个显而易见的解决方案,将文件目标文件夹作为web应用程序目录的一部分,工作进程将有适当的访问权限来保存文件。令人讨厌的部分是例外没有说明它:(像"访问被拒绝"…奇怪的例外。

希望有帮助,