来自动态源的HTML对象数据URL

时间:2022-06-01 19:53:45

So all uploads for my app are not stored in my web server space, they are stored in a file system storage. When my users want access to the file they call a URL and the backend process will buffer the data to the browser via the HttpServletResponse outputstream. This works great as intended for downloading a file. Now my use-case has a scenario where I need to load an embedded object using this same method.

因此,我的应用程序的所有上传都没有存储在我的Web服务器空间中,它们存储在文件系统存储中。当我的用户想要访问该文件时,他们调用URL,后端进程将通过HttpServletResponse输出流将数据缓冲到浏览器。这适用于下载文件。现在我的用例有一个场景,我需要使用相同的方法加载嵌入对象。

I am essentially loading a preview of the PDF file in the browser. This works fine if the PDF is stored on the web server and I provide a direct URL to the file. But when I use my method of sending files to the user then it doesn't work.

我本质上是在浏览器中加载PDF文件的预览。如果PDF存储在Web服务器上并且我提供了该文件的直接URL,则此方法可以正常工作。但是,当我使用我的方法向用户发送文件时,它不起作用。

<object data='"+pdfUrl+"' type='application/pdf' width='160px' height='160px' />

If i put pdfURL into a browser my file gets downloaded no problem. So I think the issue is the HTTP headers I am sending in the outputstream that maybe is preventing the Object from loading properly. I am not sure if maybe its expecting something specific to be set in order to trigger loading the file

如果我将pdfURL放入浏览器,我的文件下载没问题。所以我认为问题是我在输出流中发送的HTTP标头可能阻止了对象的正确加载。我不确定它是否期望为触发加载文件而设置特定的东西

I am currently using very basic headers as follows:

我目前使用非常基本的标题如下:

BufferedInputStream is = <Some File Inputstream>;
resp.setContentType(new MimetypesFileTypeMap().getContentType(directory+filename));
resp.setHeader("Content-Disposition", "attachment;  filename="+StringFormatHelper.formatFileName(filename));
bufferedCopy(is, resp.getOutputStream());
is.close();
resp.getOutputStream().flush();

Anyone have any ideas on what I have to change to get the data to properly load in the Object tag? I don't get any errors in the JS console or server side. I am not sure how to debug this issue.

任何人对我必须更改的内容有任何想法,以便在Object标记中正确加载数据?我在JS控制台或服务器端没有任何错误。我不知道如何调试此问题。

Edit: SO i just realized that if i right click on where the blank Object tag is at I have the option to "Save as..." and when I do I download the PDF. So the pdf data is loaded but Its just not displaying in the UI.

编辑:所以我刚刚意识到,如果我右键单击空白对象标签所在的位置,我可以选择“另存为...”,当我下载PDF时。所以加载了pdf数据,但它只是没有在UI中显示。

1 个解决方案

#1


0  

The issue is this line of code

问题在于这行代码

resp.setContentType(new MimetypesFileTypeMap().getContentType(directory+filename));

This was not setting the correct mime-type for the file as I thought it was. So there was a mismatch in that the Object tag was looking for application/pdf but the server was sending a different MIME type in the header. Once I matched them up everything worked.

这并没有像我想象的那样为文件设置正确的mime类型。因此,Object标签正在寻找application / pdf,但服务器在标头中发送了不同的MIME类型,这是不匹配的。一旦我匹配它们一切正常。

I was able to get the correct MIME type using the Spring provided lookup instead of the JDK lookup

我能够使用Spring提供的查找而不是JDK查找来获取正确的MIME类型

new ConfigurableMimeFileTypeMap().getContentType(directory+filename)

#1


0  

The issue is this line of code

问题在于这行代码

resp.setContentType(new MimetypesFileTypeMap().getContentType(directory+filename));

This was not setting the correct mime-type for the file as I thought it was. So there was a mismatch in that the Object tag was looking for application/pdf but the server was sending a different MIME type in the header. Once I matched them up everything worked.

这并没有像我想象的那样为文件设置正确的mime类型。因此,Object标签正在寻找application / pdf,但服务器在标头中发送了不同的MIME类型,这是不匹配的。一旦我匹配它们一切正常。

I was able to get the correct MIME type using the Spring provided lookup instead of the JDK lookup

我能够使用Spring提供的查找而不是JDK查找来获取正确的MIME类型

new ConfigurableMimeFileTypeMap().getContentType(directory+filename)