我有这段代码:
[AcceptVerbs("POST")]
[Route("api/doc/ImageUpload")]
public HttpResponseMessage ImageUpload()
{
try
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/imageUpload");
var provider = new MultipartFormDataStreamProvider(root);
Request.Content.ReadAsMultipartAsync(provider);
foreach (MultipartFileData file in provider.FileData)
{
if (string.IsNullOrEmpty(file.Headers.ContentDisposition.FileName))
{
return Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted");
}
string fileName = file.Headers.ContentDisposition.FileName;
if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
{
fileName = fileName.Trim('"');
}
if (fileName.Contains(@"/") || fileName.Contains(@"\"))
{
fileName = Path.GetFileName(fileName);
}
using (FileStream stream = File.Open(file.LocalFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//do something
}
}
}
}
这个邮递员电话:
我可以看到该文件夹中正在生成文件:
但是当试图在文件流中实际使用文件并将其另存为正确的jpeg或png时,我得到了IOException。另外我还注意到保存的文件是32kb,但是我要上传的文件是120kb。