简体   繁体   English

如何下载然后上传文件?

[英]How to download and then upload the file?

Tried to use the following code, but it doesn't work properly: 试图使用以下代码,但它无法正常工作:

  // download the file first
  var req = new XMLHttpRequest();
  req.open('GET', url, false);
  req.overrideMimeType('text/plain; charset=x-user-defined');
  req.send(null);
  if (req.status != 200) return '';

  // upload the file
  req.open("POST", "http://mysite.com/upload", false);
  req.setRequestHeader("Content-Length", req.responseText.length);
  req.sendAsBinary(req.responseText); // What should I pass here?

  if (req.status != 200) return '';
  return req.responseText;

sendAsBinary is firefox function . sendAsBinary是firefox 函数

Upd. UPD。 Also I've tried to upload that as part of the form: 我也尝试将其作为表单的一部分上传:

var response = req.responseText;
var formData = new FormData();
formData.append("file", response);
req.open("POST", "http://mysite.com/upload", false);
req.send(formData);

But still not full data is received by the server. 但是服务器仍然没有收到完整的数据。

Finally I've used the approach with temp file: 最后我用temp文件的方法:

  var downloadCompleted = false;

  // download the file first
  var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
                  .createInstance(Components.interfaces.nsIWebBrowserPersist);
  // get OS temp folder
  var file = Components.classes["@mozilla.org/file/directory_service;1"]
                .getService(Components.interfaces.nsIProperties)
                .get("TmpD", Components.interfaces.nsIFile);
  file.append("temp.ext");
  file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);

  var fURI = Services.io.newURI(url,null,null);
    const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
    const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
    persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;

  persist.progressListener = {
    onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
      },
    onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
      if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) {
         downloadCompleted = true; // file has been downloaded        
      }
    }
  }
  persist.saveURI(fURI, null, null, null, "", file);  

  var thread = Components.classes["@mozilla.org/thread-manager;1"]
                        .getService(Components.interfaces.nsIThreadManager)
                        .currentThread;
  while (!downloadCompleted) // emulate synchronous request, not recommended approach
    thread.processNextEvent(true); 

  // upload the file
  var stream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                       .createInstance(Components.interfaces.nsIFileInputStream);
  stream.init(file, 0x04 | 0x08, 0644, 0x04); // file is an nsIFile instance   

  // try to determine the MIME type of the file
  var mimeType = "text/plain";
  try {
    var mimeService = Components.classes["@mozilla.org/mime;1"]
            .getService(Components.interfaces.nsIMIMEService);
    mimeType = mimeService.getTypeFromFile(file); // file is an nsIFile instance
  }
  catch(e) { /* just use text/plain */ }

  var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
                      .createInstance(Components.interfaces.nsIXMLHttpRequest);
  req.open('POST', "http://mysite.com/upload", false); 
  req.setRequestHeader('Content-Type', mimeType);
  req.send(stream);

  // delete the file
  file.remove(false);

You need to store the responseText in an intermediate variable before reusing the req object. 在重用req对象之前,需要将responseText存储在中间变量中。

// download the file first
var req = new XMLHttpRequest();
req.open('GET', url, false);
req.overrideMimeType('text/plain; charset=x-user-defined');
req.send(null);
if (req.status != 200) return '';
var response = req.responseText;

// upload the file
req.open("POST", "http://mysite.com/upload", false);
req.setRequestHeader("Content-Length", response.length);
req.sendAsBinary(response);
if (req.status != 200) return '';
return req.responseText;

Update 更新

Per the MDN page Using XMLHttpRequest , it looks like the above code won't work. 根据MDN页面使用XMLHttpRequest ,看起来上面的代码不起作用。 Following is the proper way to get the binary response. 以下是获取二进制响应的正确方法。 In the end, you will have an array of unsigned integers which you could send back to the server and convert to binary. 最后,您将拥有一组无符号整数,您可以将它们发送回服务器并转换为二进制。 I think. 我认为。

//req.responseType is only defined for FF6+
req.responseType = "arraybuffer";
req.send(null);

//req.response is for FF6+, req.mozResponseArrayBuffer is for FF < 6
var buffer = req.mozResponseArrayBuffer || req.response;
if (buffer) {
  var byteArray = new Uint8Array(buffer);
}

Update 2 更新2

To submit the byteArray to a server, I would try something like the following untested, almost guaranteed not to work code. 要将byteArray提交到服务器,我会尝试类似以下未测试的内容,几乎保证不会工作代码。

req.open("POST", "http://mysite.com/upload", false);
req.setRequestHeader("Content-Length", byteArray.length);
//if this doesn't work, try byteArray.buffer
//if byteArray.buffer works, try skipping 'var byteArray = new Uint8Array(buffer);' altogether and just sending the buffer directly
req.send(byteArray); 

Update 3 更新3

Could Using XMLHttpRequest from JavaScript modules / XPCOM components have anything to do with your issue? 可以从JavaScript模块/ XPCOM组件中使用XMLHttpRequest与您的问题有什么关系吗?

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

相关问题 如何下载内存中的文件,然后将其上传到其他地方 - How to download file in memory and then upload it to somewhere else 如何在浏览器中上传和下载任何文件? - How to upload and download any file in the browser? 如何自定义Blueimp jQuery File Upload的上传/下载模板 - How to customize Upload/Download Template of Blueimp jQuery File Upload 如何使用文件的下载链接上传文件 - How can i upload a file by using the download link to the file 如何将文件上传到 Firebase 存储并显示下载 URL? - how can i upload file to firebase storage and display the download URL? 如何在不保存到磁盘的情况下下载和上传zip文件 - How to download and upload zip file without saving to disk 如何使用量角器通过脚本处理windows文件上传和下载? - How to handle windows file upload and download through script using protractor? 如何下载远程图像然后将图像作为图像文件上传以进行表单提交? - How to download remote image then upload image as an image file for form submission? 如何将大文本文件下载/上传到客户端应用程序 - How to download/upload large text file to client-side app 用javascript下载和上传二进制文件 - download and upload a binary file in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM