Silviu-Marian
Silviu-Marian

Reputation: 10907

How to upload an image from a remote website, by using javascript?

Is there any way I can do that? For example, I have an

<input type="file" id="upload_file" />

Obviously I can't just

$('#upload_file').val('http://www.site.com/path/to/image.jpg').parent().submit();

Upvotes: 2

Views: 1501

Answers (4)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146370

Te begin with, you cannot do file uploads without a server-side tool. Sending a file to the server is pointless if there isn't anything on the side other to receive it.

Said that, if you already have a server-side language available and a file that's publicly reachable at a URL, the server-side language is perfectly capable of fetching the file by its own means. There's no need to use the browser as intermediary. You just need to send the URL in a regular <input type="text"> element.

If the file is not publicly reachable, e.g., it belongs to an intranet or is password-protected, it's probably simpler to instruct the user to type the URL in the browser's "Open file" dialoque. In many cases, the browser will download the file and fill the <input type="file"> control with the temporary downloaded file.

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39704

IF that site supports what are you trying to do it should work. Most of file uploads are very strict, as you can't just upload a file wherever you want even if you know the upload script.

If host supports sending a link to the picture and he knows what to do with it ... I don't see why your script should not work.

<form method="post" action="http://www.example.org/">
     <input type="text" name="upload_file" id="upload_file" value="" />
     <input type="submit" onclick="$('#upload_file').val('http://www.example.org/example.jpg');">
</form>

Upvotes: 0

Andrew Peacock
Andrew Peacock

Reputation: 771

I can't see how you will get this to work. What server side language are you using? Would it not be better to just make a direct request for the remote file from the server?

Upvotes: 0

Stefan Mai
Stefan Mai

Reputation: 23939

You can't do it.

  1. Javascript won't let you read from other domains (for security reasons).
  2. File inputs don't accept URL inputs (or for that matter paths) AFAIK.

Upvotes: 4

Related Questions