xmlhttprequest 本地文件 [英] xmlhttprequest for local files

查看:78
本文介绍了xmlhttprequest 本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件的路径,我想发送到服务器的休息网络服务.我正在使用 xmlhttprequest 对象.帖子内容如下:

I have the path to a file i want to send to a rest webservice the server. I am using the xmlhttprequest object. The post is as follows:

var url = "http://localhost:8080/RestWSGS/jersey/gridsense";
 var boundary = "--------------" + (new Date).getTime();
  xmlHttp.open('POST', url, true);
  xmlHttp.onreadystatechange = function ()
  {
      if (this.readyState != 4)
        return;

      var result =this.responseText;
    document.write(result);
    };
  xmlHttp.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);

var  part ="";
 part += 'Content-Disposition: form-data; ';
  part += 'name="' + document.getElementById("filename").name + '" ; ';
  //alert(document.getElementById("filename").value);
  part += 'filename="'+ document.getElementById("filename").value +  '";\r\n';

  part += "Content-Type: application/xml";
  part += "\r\n\r\n"; // marks end of the headers part
  part += 'filename="'+ document.getElementById("filename").value +  '";\r\n';
  part+= data;
   var request = "--" + boundary + "\r\n";
  request+= part /* + "--" + boundary + "\r\n" */;
  request+= "--" + boundary + "--" + "\r\n";
  alert(request); 
  xmlHttp.send(request);  

我要发送的数据在客户端本地磁盘上.我想使用 get 方法:

The data i want to send is on the client local disk. I want to use the get method for it :

var str = document.getElementById("filename").value;


    var data;
    var xmlhttp1 = getNewHTTPObject();
    xmlhttp1.open("GET", 
    "file:///New Folder/" +document.getElementById("filename").value , false);

    xmlhttp1.send(null);
    alert('hi' + xmlhttp1.status);
    xmlhttp1.onreadystatechange = function()    {
        if (this.status == 0)
        {
            alert("resp " + this.responseText);
            data = this.responseText;
        }
    }

file://不起作用.如果我将我的文件放在客户端目录中并删除 file:///那么我至少可以看到 xmlhttprequest 打开并给出状态 200(我认为没问题!!).我读到本地文件检查状态 == 0 而不是 readystatus == 4 所以我这样做了,但它仍然将数据变量提供为未定义,因此文件不会发送到服务器.最初,当我将表单操作作为我的休息 url 时,它上传得很好.由于我没有使用 html5,因此无法从 input type=file 元素中获取 File 对象.我想为此使用 xmlhttprequest 对象而不是直接使用表单元素.请用任何建议或提示帮助我解决这个问题卡维塔

The file:// does not work. If i put my file within the client directory and remove the file:/// then i can at least see xmlhttprequest open and give status 200 (i think ok!!). I read that for local file check status == 0 instead of readystatus == 4 so i did that but it still gives data variable as undefined and so the file does not go to the server. Initially when i had given the form action as my rest url it was uploading fine. Since I am not using html5 i cannot get the File object from the input type=file element. I want to use the xmlhttprequest object for this instead of the form element directly. Please help me with this problem with any suggestions or hints KAvita

即使我使用表单提交进行上传,我如何使用网络服务的返回值.这就是我需要使用 xmlhttpRequest 的原因.如果有人可以建议如何使用操作的返回值,那就太好了!!卡维塔

Even if i do the uploading using form submission how can i use the return value of the web service. Thats the reason I need to use xmlhttpRequest. If anyone can suggest how the return value from the action is used it will be great!! Kavita

推荐答案

从历史上看,您不能从 JavaScript 查询本地文件(或者不应该被允许,或者有些奇怪).这将严重破坏安全.

Historically, you can't query for local files from JavaScript (or shouldn't be allowed to, or something's odd). This would be a serious breach of security.

只有少数几种情况可以执行此操作,但通常它们涉及需要为浏览器设置的特定安全设置,以解除限制或通知当前页面的执行过程已被授予此权限特殊的权利.例如,这可以通过编辑属性在 Firefox 中实现.在开发浏览器扩展程序(例如 Chrome 或 FF)时,如果他们请求文件访问权限,通常也可以.

There are only a few circumstances where you can do this, but in general they involve specific security settings requiring to be set for your browser, to either lift the limitation or to notify the current page's execution process that that is is granted this exceptional right. This is for instance doable in Firefox by editing the properties. It's also commonly OK when developing browser extensions (for instance for Chrome or FF) if they request the file access permissions.

解决此限制的另一种方法是托管本地 Web 服务器,并在其上声明虚拟主机,以便能够执行此类 AJAX 请求以获取本地文件.Web 开发人员采用这种技巧(实际上更像是一种标准)来获得本地开发的好处,但同时复制生产系统是很常见的.例如,您可以使用像 Jetty 这样的轻量级网络服务器.

Another way to go around this limitation is to host a local web-server, and to declare virtual hosts on it to be able to do this sort of AJAX request to fetch local files. It's quite common for web-developers to resort to this trick (more like a standard, really) to have the benefits of local development but at the same time replicate a production system. You could for instance use a lightweight web-server like Jetty.

(您似乎遇到的另一个烦恼是,某些浏览器(至少是一些相对较旧的 FF 版本,例如 3.6.x)在请求被阻止时有时会返回正错误代码,例如 200根据他们的内部安全政策.可能会令人困惑一段时间......).

最后,较新的 HTML5 API 确实提供了一些用于访问本地文件的新结构.考虑阅读:

Finally, the newer HTML5 APIs do provide some new constructs to access local files. Considering reading:

其他 SO 问题也提供了额外的提示:

Other SO questions also provide additional pointers:

这篇关于xmlhttprequest 本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆