相关疑难解决方法(0)

如何解决"超出最大请求长度"异常?

当我上传图片时出现此错误:

超出最大请求长度

我该如何解决这个问题?

asp.net

27
推荐指数
3
解决办法
7万
查看次数

使用MultipartFormDataStreamProvider和ReadAsMultipartAsync

我将如何使用MultipartFormDataStreamProviderRequest.Content.ReadAsMultipartAsync使用ApiController

我已经google了一些教程,但我无法让它们中的任何一个工作,我使用.net 4.5.

这就是我目前得到的:

public class TestController : ApiController
{
    const string StoragePath = @"T:\WebApiTest";
    public async void Post()
    {
        if (Request.Content.IsMimeMultipartContent())
        {
            var streamProvider = new MultipartFormDataStreamProvider(Path.Combine(StoragePath, "Upload"));
            await Request.Content.ReadAsMultipartAsync(streamProvider);
            foreach (MultipartFileData fileData in streamProvider.FileData)
            {
                if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName))
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
                }
                string fileName = fileData.Headers.ContentDisposition.FileName;
                if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                {
                    fileName = fileName.Trim('"');
                }
                if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                {
                    fileName = Path.GetFileName(fileName);
                }
                File.Copy(fileData.LocalFileName, Path.Combine(StoragePath, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-web-api

19
推荐指数
2
解决办法
4万
查看次数

标签 统计

asp.net ×2

asp.net-web-api ×1

c# ×1