json 将PDF从控制器返回到视图时出现ReadTimeOut异常

nkkqxpd9  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(146)

当用户点击打印按钮时,我想下载PDF。
我的控制器方法:

[System.Web.Http.HttpGet]
        public FileStreamResult CreatePDF()
        {
            MemoryStream workStream = new MemoryStream();
            Document document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();
            document.Add(new Paragraph("Hello World"));
            document.Add(new Paragraph(DateTime.Now.ToString()));
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return new FileStreamResult(workStream, "application/pdf");
        }

字符串
这是我从视图中调用它的方式:

$('#print').click(function (evt) {
                window.location.href = "/api/pdfdownload/createpdf";
            });


点击打印按钮时出现以下错误:
“ObjectContent”% 1“类型无法序列化内容类型”application/json“的响应正文; charset=utf-8 '. System.InvalidOperationException发生错误。从“System.IO. MemoryStream”上的“ReadList”获取值时出错。Newtonsoft.Json.JsonSerializationException位于Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象目标)位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer,Object value,JsonContainerContract contract,JsonProperty member,JsonProperty property property,JsonContract& memberContract,Object& memberValue)at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)位于Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject上的JsonProperty containerProperty)(JsonWriter writer,Object value,JsonObjectContract合约,JsonProperty成员,JsonContainerContract collectionContract,Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue上的JsonProperty containerProperty)(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty)at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value,Type objectType)at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value,Type objectType)at System.Net.Http.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,Encoding effectiveEncoding)at System.Net.Http.JsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,Encoding effectiveEncoding)at System.Net.Http.BaseJsonMediaTypeFormatter.WriteToStream(类型类型、对象值、流writeStream、HttpContent内容)位于System.Net.Http.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型,对象值,流writeStream,HttpContent内容,TransportContext,cancellationToken cancellationToken)-从抛出异常的上一个位置开始的堆栈跟踪结束-在System. System. Data. DataServices.TaskAwaiter.ThrowForNonSuccess(任务任务)位于System. System. Data. DataServices.TaskAwaiter. HandleNoncalling和NoncallinggerNotification System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()中的(任务任务)发生错误。此流不支持超时。Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(对象目标)中的System.IO.Stream.get_ReadError()中的System.InvalidOperationException(对象)
我只是不明白这个例外在这种情况下意味着什么。将感谢您的帮助。

ztigrdn8

ztigrdn81#

这意味着它无法将MemoryStream类型的对象序列化为JSON:
“ObjectContent”1“类型无法序列化内容类型”application/json; charset=utf-8“的响应正文。System.InvalidOperationException出现错误。从 ”System.IO. MemoryStream“上的”ReadList“获取值时出错。Newtonsoft. Json. JsonSerializationException...*
我的猜测是,你正在尝试,也许是有意的,也许不是,将MemoryStream序列化为JSON,这并不是突然的,因为你正在处理PDF文件,而你不能这样做。MemoryStream包含字节,不能序列化为JSON。
我建议你看看这个post从asp.net论坛,但没有更多的信息,这是所有有生产。

相关问题