javascript - 我可以使用任何 HTML 或 JavaScript API 在 input[type=file] 中获取文件的路径吗?

标签 javascript file upload path

我想实现一个上传图片功能。

上传后,我想获取文件本地路径,以便创建缩略图。但是我怎样才能得到文件路径呢?还有另一种创建缩略图的方法吗?

我试过 jQuery-File-Upload 插件。

最佳答案

jQuery File Upload Demo Page演示获取要上传的文件名的概念。添加文件时,文件名显示在控件下方的表格 View 中。

该插件有几个选项,包括在特定事件发生后触发的回调。

这是一个显示如何获取文件名的示例:

function (e, data) {
    $.each(data.files, function (index, file) {
        alert('Added file: ' + file.name);   // filename alerted
    });
    data.url = '/path/to/upload/handler.json';
    var jqXHR = data.submit()
        .success(function (result, textStatus, jqXHR) {/* ... */})
        .error(function (jqXHR, textStatus, errorThrown) {/* ... */})
        .complete(function (result, textStatus, jqXHR) {/* ... */});
}

根据文档,如果您使用 .bind 绑定(bind)回调函数,您将有最大的成功机会。 jQuery的关键字:
$('#fileupload')
   .bind('fileuploadadd', function(e, data) { ... } );

来自关于“添加”的文档:

The data parameter allows to override plugin options as well as define ajax settings. data.files holds a list of files for the upload request: If the singleFileUploads option is enabled (which is the default), the add callback will be called once for each file in the selection for XHR file uploads, with a data.files array length of one, as each file is uploaded individually. Else the add callback will be called once for each file selection.



这是jQuery File Upload Options的完整列表.

此外,只有部分浏览器支持图像预览,这是您访问文件名的目标:

Image previews

The following browsers have support for image previews prior to uploading files:

  • Firefox 3.6+
  • Google Chrome
  • Opera 11+


Browser Support页面详细说明了插件的哪些功能在不同的浏览器中受支持和不受支持。

关于javascript - 我可以使用任何 HTML 或 JavaScript API 在 input[type=file] 中获取文件的路径吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10084133/

相关文章:

javascript - document.addEventListener 与 $(document).on

C读取二进制文件

node.js - gcloud 错误 : ApiError: Not Found at new util. ApiError

Javascript mouseDown - 无法读取未定义的 currentTarget

javascript - 如何在 RXJS 订阅中运行多个条件

javascript - 减少包含对象而不是整数的多维数组

c++ - 简单的 Windows C++ 不可见

java - 将远程服务器目录复制到本地计算机

php - 如何使用 PLupload 发送附加数据?

django - 使用 django + nginx + apache mod_wsgi 访问正在进行的上传文件