简体   繁体   中英

jquery/javascript: download a file from any webserver

I'm creating a JS/JQUERY Web Application that needs to be able to download a 3d geometry file from any server, and then display that file in the window. The following function works for this purpose locally:

function file_download(fileName, callback)
{
   var model = new Object();

   $.ajax(
   {
      url: fileName,
      dataType: 'text',
      success: function(data) 
      {
          model = new obj_create(data);
          callback(model); 
      }
    });
}

However, whenever I attempt to input a URL that is outside the server, I get the following error:

XMLHttpRequest cannot load '<URL HERE>' No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<MY LOCAL SERVER'S URL>' is therefore not allowed access. 

The file can be opened manually in my browser, but cannot be automatically grabbed via ajax.

I have researched trying to do this with a CORS request, but from what I can see this would require me to be able to edit permissions on the target server. I need to be able to download the file from an arbitrary webserver.

I've seen this done before for image uploading in forums, for choosing user icons. Can this be done with JS and Jquery? I can use PHP if I must, but I've been asked to avoid it.

If you want to do a cross-domain invocation, you'll have to take a look at this:

Cross-Origin Resource Sharing (CORS) is a W3C spec that allows cross-domain communication from the browser. By building on top of the XMLHttpRequest object, CORS allows developers to work with the same idioms as same-domain requests.

http://www.html5rocks.com/en/tutorials/cors/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM