C# CustomMultipartstreamprovider引发错误和异常
我正在使用asp.NETMVC4进行服务。我需要能够接受来自客户端应用程序的已发布图像/文件。我写了一个代码,它工作得很好,但现在2到3周后,它不工作,显示异常 CustomMultipartstreamprovider引发异常 不知道是什么问题,请帮忙 我的代码是C# CustomMultipartstreamprovider引发错误和异常,c#,asp.net,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc 4,我正在使用asp.NETMVC4进行服务。我需要能够接受来自客户端应用程序的已发布图像/文件。我写了一个代码,它工作得很好,但现在2到3周后,它不工作,显示异常 CustomMultipartstreamprovider引发异常 不知道是什么问题,请帮忙 我的代码是 public class UploadController : ApiController { private static IExceptionLogs _iException = new Exceptio
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
}
}
在我的例子中,保存文件的文件上载目标文件夹与Web应用程序目录链的虚拟目录不同,出现此异常。作为一个明显的解决方案,将文件目标文件夹作为web应用程序目录的一部分,工作进程将具有保存文件的正确访问权限。 令人讨厌的是,特例并没有说明这一点:比如“拒绝访问”。。。奇怪的例外
希望有帮助,还有什么异常?CustomMultipartFormDataStreamProvider类型的流提供程序引发了异常。这是例外@Dhavl Patelwhat您在路径中得到了什么?抱歉???你说的路径是什么意思?CustomMultipartFormDataStreamProvider类字符串路径我指的是那个路径