html5获取文件路径 [英] html5 get file path

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

问题描述

使用html5 javascript,当用户选择文件时,如何获取文件路径?
我需要在这个示例中使用的文件路径:

Using html5 javascript, how do you get the file path when user select the file? I needed the file path to use in this example case:

用户上传文件,暂停它(我知道到目前为止我认为只有mozilla可以这样做),然后关闭浏览器并计划第二天恢复该文件。我需要知道这个文件的文件路径..

user upload a file, pause it(I know so far I think only mozilla can do this), and then close the browser and plan to resume the file the next day. I need to know the file path for this file..

推荐答案

即使你确实有一个路径(一些浏览器曾经给过它给你),没有办法设置类型文件的输入路径。

Even if you did have a path (some browsers used to give it to you), there is no way to set the path of a input of type file.

因此,你不可能做你想要的普通JS和DOM。

我说这是不可能的,但现在你问我确实有办法,使用新的File API 。以下步骤概述了需要做什么,但没有经过测试,我不希望它能够工作,它只是向您展示方式,全局变量也是坏的,它只是向您展示最简单的方式。这是一个很好的页面,其中包含使用文件API的示例 http://www.html5rocks.com / en / tutorials / file / dndfiles /

I said it wasn't possible, but now that you asked I do think there is a way, with new File API. The following steps outline what needs to be done, but have in no way been tested, and I don't expect it to work, it's just to show you the way, also global variables are bad, it's just the simplest way to show you. Here's a good page with examples on using the File API http://www.html5rocks.com/en/tutorials/file/dndfiles/

首先您需要输入类型文件,然后您可以使用一次用户选择一个文件,您就可以访问File对象。 http://dev.w3.org/2006/webapi/FileAPI/#dfn -filereader

First You need an input of type file, then you can use once a user selects a file, you are given access to the File object. http://dev.w3.org/2006/webapi/FileAPI/#dfn-filereader

<input type="file" id="files" name="files[]" multiple />
<script>
  var fileBlobs = [];
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // files is a FileList of File objects. You may now read their contents
    var reader = new FileReader();
    for (var i = 0, f; f = files[i]; i++) {
      fileBlobs.push(reader.readAsBinaryString(f));
    }
  }    
  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

现在您将内容作为二进制字符串。您可以使用File API以稍后可以使用FileWriter和FileSaver接口访问的方式在本地存储文件 http://dev.w3.org/2009/dap/file-system/file-writer.html

Second Now you have the content as a binary string. You can use the File API to store the file locally in a way that you can access later using the FileWriter and FileSaver interfaces http://dev.w3.org/2009/dap/file-system/file-writer.html

var bb = new BlobBuilder();
bb.appendfileBlobs.(fileBlobs[0]);
window.saveAs(bb.getBlob(), "test_file");

第三您需要发出一个Ajax请求将该blob传递给服务器,跟踪上传完成的程度。 http://www.w3.org/TR/XMLHttpRequest/#the-upload -attribute 。它看起来不像在客户端上可以进行跟踪进度。您可能需要轮询服务器以获取实际进度。知道在哪里开始中断上传的可能解决方案是使用上传ID,当您重新启动上传时,您会询问服务器已上传了多少文件。

Third You need to make an Ajax request passing that blob to the server, tracking how much of the upload is complete. http://www.w3.org/TR/XMLHttpRequest/#the-upload-attribute. It doesn't look like tracking progress can be done on the client. Your may have to poll the server for actual progress. A possible solution to know where to start an interrupted upload is to have upload ids and when you restart an upload, you ask the server how much of the file has been uploaded.

对不起,我无法提供完整的答案,但这不是一个简单的问题。我给你的步骤应该带你朝正确的方向发展。

Sorry I can't provide a full answer, but this is not an easy problem. The steps I gave you should take you in the right direction.

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

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