简体   繁体   English

有效负载(空内容类型)禁止错误| 从ember的服务器下载pdf文件时

[英]Payload (Empty Content-Type) Forbidden Error | While downloading a pdf file from the server in ember

Objective: Want to download a PDF file from the server by generating the below URL: http://localhost:4200/bookStore/secured/rest/books/14119 目标:希望通过生成以下URL从服务器下载PDF文件: http:// localhost:4200 / bookStore / secured / rest / books / 14119

The URL gets generated when the below line gets executed in the book.js in routes. 在book.js中的路线中执行以下行时,将生成URL。

return this.store.findRecord('book', bookId);

But I get 403 forbidden error. 但是我收到403禁止错误。 The error in details: 错误的详细信息:

Error: Ember Data Request GET /bookStore/secured/rest/books/14119 returned a 403
Payload (Empty Content-Type)
Forbidden
    at new AdapterError (-private.js:3170)
    at Class.handleResponse (rest.js:594)
    at ajaxError (rest.js:956)
    at Class.hash.error (rest.js:623)
    at fire (jquery.js:3317)
    at Object.fireWith [as rejectWith] (jquery.js:3447)
    at done (jquery.js:9274)
    at XMLHttpRequest.<anonymous> (jquery.js:9514)

Do I need to set the content-type as 'application/pdf'? 我需要将内容类型设置为“ application / pdf”吗? and if yes, then please suggest how to set it. 如果是,那么请建议如何设置。

Below is the JS code: 下面是JS代码:

routes\\book.js 路线\\ book.js

actions: {
    pdfClick(bookId) {
      return this.store.findRecord('book', bookId);
   }
}

template\\book.hbs template \\ book.hbs

<button {{action "pdfClick" book.bookId}}>PDF</button>

In the server side, the code which receive and respond the request is as follows: 在服务器端,接收和响应请求的代码如下:

@GET
@Path("/{bookId}")
@Produces("application/pdf")
public Response bookExport(@PathParam("bookId") long bookId) {
   //Code
}

According to the server side code, we are getting PDF file from the server. 根据服务器端代码,我们正在从服务器获取PDF文件。 This is tested successfully using PostMan. 使用PostMan已成功测试。

It looks like you are trying to use Ember Data to download the PDF file. 您似乎正在尝试使用Ember Data下载PDF文件。 Ember Data is mostly meant to handle JSON data though, so this might not be the best fit in this case. 尽管Ember Data主要用于处理JSON数据,所以在这种情况下这可能不是最合适的选择。 You could still keep using Ember Data for everything else, but for downloading the PDF I'd recommend to only use it to generate the correct URL. 您仍然可以继续将Ember Data用于其他所有内容,但是对于下载PDF,我建议仅使用它来生成正确的URL。

You should be able to generate the URL roughly like this: 您应该能够大致像这样生成URL:

let url = this.store.adapterFor('book').urlForFindRecord(bookId, 'book');

or just: 要不就:

let url = `http://localhost:4200/bookStore/secured/rest/books/${bookId}`;

if you don't want to use Ember Data for this at all. 如果您根本不想使用Ember Data。

Finally to download the PDF you could use: 最后下载PDF,您可以使用:

document.location.assign(url);

or convert your button to a link: 或将您的按钮转换为链接:

<a href={{url}}>Download PDF</a>

You are doing a request to http://localhost:4200/bookStore/secured/rest/books/14119 , but this is the Ember development server. 您正在向http://localhost:4200/bookStore/secured/rest/books/14119 ,但这是Ember开发服务器。 If you use ember data, you can customize the host in the adapter. 如果使用余烬数据,则可以在适配器中自定义主机。 See this section of the documentation for how to do that. 有关如何执行此操作,请参阅文档的本部分

Note that Ember Data does not know how to deal with PDF files automatically. 请注意,Ember Data不知道如何自动处理PDF文件。 If you just want to download it, I suggest just linking to it directly with an <a href="location of pdf file" . 如果您只想下载它,建议您直接使用<a href="location of pdf file"链接到它。

You want to retrieve a binary (pdf) as response to a rest call. 您想要检索二进制(pdf)作为对rest调用的响应。 You have several different options: 您有几种不同的选择:

First one is the same with TBienek's <a href={{url}}>Download PDF</a> aproach. 第一个与TBienek的<a href={{url}}>Download PDF</a> I think it is the simplistic and cross-browser solution. 我认为这是一种简单的跨浏览器解决方案。 This will eleminate the need to rest call. 这将消除休息电话的需要。

Second one is to send pdf bytes in a json object such as: 第二个方法是在json对象中发送pdf字节,例如:

{
  content:'<base64 encoded byte arr>',
  filename:'<string>,
  contentType: 'application/pdf'
}

After that create a Blob with content and trigger to open it (via window.open ). 之后,创建一个包含contentBlob并触发将其打开(通过window.open )。

There is also a third alternative using with XMLHttpRequest without changing your current server endpoint. 在不更改当前服务器端点的情况下,还有第三种与XMLHttpRequest结合使用的方法。 Use the response of XMLHttpRequest as content in the second option. 在第二个选项中,将XMLHttpRequest的response用作content

None of them has a usage of Ember Data. 他们都没有使用Ember Data。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下载内容类型为 Content-Type:multipart/mixed 的文件 - Downloading a file with content type Content-Type:multipart/mixed 清空没有Content-type的$ _POST - Empty $_POST without Content-type 从Javascript返回具有Content-type的文件内容 - Return file contents with Content-type from Javascript 阻止通过Chrome扩展程序按内容类型下载 - Block downloading by Content-Type via Chrome Extension 如果我得到应用程序流的 Axios 响应内容类型,如何保存 PDF 格式文件? - How to save PDF format file if I get the Axios response content-type of application-stream? 通过 jQuery 下载时获取空 PDF - Getting empty PDF while downloading through jQuery jsp文件的内容类型是什么? - What is the content-type of a jsp file? 如何解决在 D3JS 中创建和下载文件时出现“Failed - Forbidden”错误 - How to resolve the “Failed - Forbidden” error while creating and downloading a file in D3JS 使用angular-file-upload从请求正文中删除Content-Disposition和Content-Type - Remove Content-Disposition and Content-Type from request body using angular-file-upload 从服务器下载文件时获取`tar: Error opening archive: Unrecognized archive format` - Getting `tar: Error opening archive: Unrecognized archive format` while downloading a file from server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM