如何解决上传转换为 base64 的 pdf 时返回内部服务器错误代码并使用 json ASP.NET MVC 发布
我正在尝试使用 ASP.NET MVC 5 创建一个 api,该 api 上传转换为 base64 的 pdf,但返回 StatusCode:500 内部服务器错误。 base64 的长度为 926,215 个字符。 这是我的控制器:
using Newtonsoft.Json;
using System;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Configuration;
using System.Net.Http.Headers;
[HttpPost]
public ActionResult PDFUpload(PDFDetails rd)
{
if (ModelState.IsValid)
{
if (rd.File != null && rd.File.ContentLength > 0 && rd.File.ContentType == "application/pdf")
{
using (var client = new HttpClient())
{
//http://ptsv2.com/t/syaz0-1615714235/post
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["TestUrl"]);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var fileString = new
{
file = new
{
mime = rd.File.ContentType,data = "base64-data=" + ToBase64(rd.File)
}
};
var json = JsonConvert.SerializeObject(fileString);
var postTask = client.PostAsJsonAsync("post",json);
postTask.Wait();
if (postTask.IsCompleted)
{
Console.WriteLine("complete");
}
}
}
}
return RedirectToAction("SubmitDetails");
}
这是我将文件转换为 base64 的方法
public static String ToBase64(HttpPostedFileBase cv)
{
byte[] fileInBytes = new byte[cv.ContentLength];
using (BinaryReader theReader = new BinaryReader(cv.InputStream))
{
fileInBytes = theReader.ReadBytes(cv.ContentLength);
}
string fileAsString = Convert.ToBase64String(fileInBytes);
return fileAsString;
}
我的课
public class PDFDetails
{
public HttpPostedFileBase File { get; set; }
}
当我只删除 base64 编码的字符串时它就起作用了。所以我尝试增加请求并添加超时但仍然有相同的错误。
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="TestUrl" value="http://ptsv2.com/t/syaz0-1615714235/"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647"/>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule,Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
<remove name="OPTIONSVerbHandler"/>
<remove name="TRACEVerbHandler"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
<modules>
<remove name="TelemetryCorrelationHttpModule"/>
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule,Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler"/>
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule,Microsoft.AI.Web" preCondition="managedHandler"/>
</modules>
<validation validateIntegratedModeConfiguration="false"/>
<security>
<requestFiltering>
<!-- This will handle requests up to 1024MB (1GB) -->
<requestLimits maxAllowedContentLength="1048576000"/>
</requestFiltering>
</security>
</system.webServer>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。