0

Suppose I have a Google url of a pdf file, is there anyway in javascript to get the data of a file and send it via ajax (json) ?

This question is not about the AJAX, but getting the data from a file retrieved via. a link. Link shown below.

enter image description here

I'll explain in more detail: I plan to build a chrome extension that upon clicking a google result link (of a pdf file for example) will get the data of the link (pdf file data).

How do I do so in javascript if it's possible?

jmarkmurphy
  • 11,030
  • 31
  • 59
Midnight_Blaze
  • 481
  • 6
  • 29
  • "This question is not about the AJAX" — Yes, it is. – Quentin Apr 18 '17 at 11:45
  • 2
    "getting the data of a file downloadable in a link" — The link describes where to get the data using HTTP. To get data using HTTP you must make an HTTP request. You want to do it with JavaScript. "Making an HTTP request with JavaScript" is what AJAX means. – Quentin Apr 18 '17 at 11:46
  • you're right, I asked cause I didn't know, thanks for clarifying. The explanation does make a lot of sense. – Midnight_Blaze Apr 18 '17 at 11:47

1 Answers1

1

EDIT: The question was edited, so I'll have to update my answer.

I suppose by "sending it via AJAX", you mean downloading to the client by sending the headers via AJAX.

The short answer is, no, you won't be able to.

An explanation can be found here. Download a file by jQuery.Ajax

Bluish is completely right about this, you can't do it through Ajax because JavaScript cannot save files directly to a user's computer (out of security concerns). Unfortunately pointing the main window's URL at your file download means you have little control over what the user experience is when a file download occurs.


You can use FormData objects to send files via AJAX. A little Google will find you the answer:

jQuery Ajax File Upload

Check out the second answer in the above link:

Iframes is no longer needed for uploading files through ajax. I've recently done it by myself. Check out these pages:

Using HTML5 file uploads with AJAX and jQuery

http://dev.w3.org/2006/webapi/FileAPI/#FileReader-interface

Community
  • 1
  • 1
Lin De
  • 54
  • 3
  • That's for reading **local** files selected from a **file input** by the user. The question is asking how getting data from a remote server using HTTP. – Quentin Apr 18 '17 at 11:48
  • @Quentin Thanks for pointing it out, I haven't seen the updated question. Just corrected my answer. – Lin De Apr 18 '17 at 12:09