简体   繁体   English

试图从 ASP.NET Web API 获取文件响应

[英]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.

相关问题 尝试从asp.net Web API调用获取响应时,Android Studio中出现超时异常 - Timeout exception in android studio while trying to get a response from an asp.net web api call 无法从ASP.NET Web API获得响应 - Can't get response from ASP.NET web API 从Asp.net Web Api控制器获取文件名 - Get file name from Asp.net Web Api controller 无法从 ASP.NET Web API 获得任何响应 - Can't get any response from ASP.NET Web API 试图从asp.net Web API帖子中获取正确的JSON格式 - Trying to get right JSON format from a asp.net Web API post ASP.Net Web API - 同步获取POSTed文件? - ASP.Net Web API - Get POSTed file synchronously? 如何从Asp.Net Web Api 2获得类似于Google API响应的JSON结果? - How can I get JSON result from Asp.Net Web Api 2 that looks like Google API response? Sending class data as JSON array format for GET request Response in ASP.Net Dot Core Web API ( GET response data from Web API) - Sending class data as JSON array format for GET request Response in ASP.Net Dot Core Web API ( GET response data from Web API) 从列表中获取一个项目<claim>在 ASP.NET Web API</claim> - Get an item from List<Claim> in ASP.NET Web API 从ASP.NET页中的Web Api Controller获得价值 - Get value from Web Api Controller in ASP.NET page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM