[英]MVC4 FileResult PDF corruption
I am transmitting a PDF to an MVC controller, saving the PDF to a database, and retrieving it. 我正在将PDF传输到MVC控制器,将PDF保存到数据库,然后检索它。 If I save the PDF to disk on the server side (prior to returning a file result), it looks fine.
如果我将PDF保存到服务器端的磁盘上(在返回文件结果之前),则看起来很好。 However, when I do something akin to the following:
但是,当我执行以下操作时:
return File(fileBytesFromDB, "application/pdf", "file.pdf");
and try to view it from a browser (any browser), the PDF appears to be corrupt. 并尝试从浏览器(任何浏览器)查看它,PDF似乎已损坏。 In fact, it's an entirely different size.
实际上,它的大小完全不同。
What have I missed? 我错过了什么?
I found it. 我找到了。 One of my coworkers had added in a tokenizer as a decorator of return actions, and it was tokenizing the file stream.
我的一位同事添加了一个标记器,作为返回动作的修饰符,它正在标记文件流。 I added some logic to ignore
FileStreamResult
and FileContentResult
and now the PDFs come out okay. 我添加了一些逻辑来忽略
FileStreamResult
和FileContentResult
,现在PDF可以正常显示了。
I also found another solution to this. 我还找到了另一种解决方案。 In our DB, our table was storing the filepath, name, mimetype as nchar(255) which is a fixed character count.
在我们的数据库中,我们的表将文件路径,名称,mimetype存储为nchar(255),这是一个固定的字符数。 The added whitespace was not working with the FileResult method even using .Trim().
即使使用.Trim(),添加的空格也无法与FileResult方法一起使用。 My particular issue was solved by changing the column to varchar
通过将列更改为varchar解决了我的特殊问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.