[英]Trying to get file response from ASP.NET Web API
I have migrated some methods from a MVC controller to a Web API controller and now I have this method:我已经将一些方法从 MVC controller 迁移到 Web API controller 现在我有了这个方法:
[HttpPost]
[Route("api/Request/UploadImage")]
public IHttpActionResult UploadImage()
{
try
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var httpRequest = System.Web.HttpContext.Current.Request;
if (_inMemoryStore == null)
{
_inMemoryStore = new List<FileLocalStore>();
}
if (httpRequest.Files.Count > 0)
{
var postedFile = httpRequest.Files[0];
var uniqueFileName = Guid.NewGuid().ToString();
var fileStream = new MemoryStream();
postedFile.InputStream.CopyTo(fileStream);
_inMemoryStore.Add(new FileLocalStore() { Id = uniqueFileName, File = fileStream });
var fileStore = new ServiceRequestAttachmentViewModel
{
FileName = httpRequest.Form["FileName"].ToString(),
FileMIME = httpRequest.Form["FileMIME"].ToString(),
UniqueFileName = uniqueFileName,
Thumbnail = fileStream.GetBuffer().GetThumbnailImage(80, 80),
IsPrivate = Convert.ToBoolean(httpRequest.Form["IsPrivate"].ToString()),
IsAdded = true,
IsDeleted = false
};
var content = JsonConvert.SerializeObject(fileStore);
// return Ok(fileStore);
return Content(HttpStatusCode.OK,fileStore);
}
else
{
return Ok(new { Data = "" });
//return Request.CreateResponse(HttpStatusCode.Created, new
//{ Data = "" });
}
}
catch (Exception ex)
{
Log.Error($"Error uploading image {ex.Message} {ex.InnerException} {ex.StackTrace}");
return BadRequest(ex.Message);
//var response2 = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
//return response2;
}
}
In the original MVC controller, it was easy to return the ContentResult
after fileStore
was serialized.在原来的MVC controller中,很容易将fileStore
序列化后返回ContentResult
。 I want to do the same thing here but I'm having issues.我想在这里做同样的事情,但我遇到了问题。 It keeps saying it exceeds the maximum bytes but the file was only 10k big.它一直说它超过了最大字节数,但文件只有 10k 大。
Is there something else I need to set?还有什么我需要设置的吗? the media type received is a multipart/form-data
type.接收到的媒体类型是multipart/form-data
类型。 The thumbnail property is the issue as it has bytes of data. thumbnail 属性是个问题,因为它有数据字节。
This is being called using fileupload()
method of jQuery.这是使用 jQuery 的fileupload()
方法调用的。
You probably.net to update the maxRequestLength & maxAllowedContentLength in web.config您可能.net 更新 web.config 中的 maxRequestLength 和 maxAllowedContentLength
From MSDN , the maximum default size is 4MB来自MSDN ,最大默认大小为 4MB
Here's the setting for 1GB这是1GB的设置
<system.web>
<httpRuntime maxRequestLength="2097152" requestLengthDiskThreshold="2097152" executionTimeout="240"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
</system.webServer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.