我正在使用asp.net mvc 4进行服务。我需要能够从客户端应用程序接受POSTED图像/文件。我写了一个代码,它工作正常,但现在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
}
}
答案 0 :(得分:0)
在我的情况下,文件上载目标文件夹(保存文件)与Web应用程序目录链的虚拟目录不同,发生此异常。作为一个明显的解决方案,将文件目标文件夹保留为Web应用程序目录的一部分,工作进程将具有保存文件的适当访问权限。 讨厌的部分是例外,不说它:(像'拒绝访问'......奇怪的例外。
希望它有所帮助,