It seems like I have not clearly communicated my problem. I need to send a file (using AJAX) and I need to get the upload progress of the file using the Nginx HttpUploadProgressModule. I need a good solution to this problem. I have tried with the jquery.uploadprogress plugin, but I am finding myself having to rewrite much of it to get it to work in all browsers and to send the file using AJAX.

我所需要的只是代码,它需要在所有主要浏览器(Chrome、Safari、FireFox和IE)中工作.如果我能找到一个能够处理多个文件上传的解决方案,那就更好了.

I am using the jquery.uploadprogress plugin to get the upload progress of a file from the NginxHttpUploadProgressModule. This is inside an iframe for a facebook application. It works in firefox, but it fails in chrome/safari.

当我打开控制台时,我看到了这个.

Uncaught ReferenceError: progressFrame is not defined
jquery.uploadprogress.js:80

你知道我该怎么解决这个问题吗?

我还想在文件完成后使用AJAX发送文件.我将如何实现这一点?

EDIT:
I need this soon and it is important so I am going to put a 100 point bounty on this question. The first person to answer it will receive the 100 points.

EDIT 2:
Jake33 helped me solve the first problem. First person to leave a response with how to send the file with ajax too will receive the 100 points.

推荐答案

如今,使用AJAX上传文件实际上是可能的.是的,是AJAX,而不是像swf或java这样的蹩脚AJAX爱好者.

This example might help you out: https://webblocks.nl/tests/ajax/file-drag-drop.html

(It also includes the drag/drop interface but that's easily ignored.)

Basically what it comes down to is this:

<input id="files" type="file" />

<script>
document.getElementById('files').addEventListener('change', function(e) {
    var file = this.files[0];
    var xhr = new XMLHttpRequest();
    (xhr.upload || xhr).addEventListener('progress', function(e) {
        var done = e.position || e.loaded
        var total = e.totalSize || e.total;
        console.log('xhr progress: ' + Math.round(done/total*100) + '%');
    });
    xhr.addEventListener('load', function(e) {
        console.log('xhr upload complete', e, this.responseText);
    });
    xhr.open('post', '/URL-HERE', true);
    xhr.send(file);
});
</script>

(演示:http://jsfiddle.net/rudiedirkx/jzxmro8r/)

所以基本上归结起来就是=)

xhr.send(file);

其中fileBlob:http://www.w3.org/TR/FileAPI/的类型

Another (better IMO) way is to use FormData. This allows you to 1) name a file, like in a form and 2) send other stuff (files too), like in a form.

var fd = new FormData;
fd.append('photo1', file);
fd.append('photo2', file2);
fd.append('other_data', 'foo bar');
xhr.send(fd);

FormData使服务器代码更干净、更向后兼容(因为现在请求的格式与普通表单完全相同).

All of it is not experimental, but very modern. Chrome 8+ and Firefox 4+ know what to do, but I don't know about any others.

以下是我在PHP中处理请求(每个请求一个图像)的方式:

if ( isset($_FILES['file']) ) {
    $filename = basename($_FILES['file']['name']);
    $error = true;

    // Only upload if on my home win dev machine
    if ( isset($_SERVER['WINDIR']) ) {
        $path = 'uploads/'.$filename;
        $error = !move_uploaded_file($_FILES['file']['tmp_name'], $path);
    }

    $rsp = array(
        'error' => $error, // Used in JS
        'filename' => $filename,
        'filepath' => '/tests/uploads/' . $filename, // Web accessible
    );
    echo json_encode($rsp);
    exit;
}

Jquery相关问答推荐

如何在html css中制作响应表

更改高亮 colored颜色

缺少 .map 资源?

在学习 jQuery 之前学习 JavaScript 是个好主意吗?

jQuery 与 ExtJS

JavaScript 中的循环计时器

使用 JQuery 清除下拉列表

JSON字符串到JS对象

如何使用 jQuery 清空输入值?

实用的 javascript 面向对象设计模式示例

jQuery .live() 和 .on() 有什么区别

Jquery .show() 不显示隐藏可见性的 div

.apply jQuery 函数是什么?

我如何知道 jQuery 是否有待处理的 Ajax 请求?

jQuery select2 获取 Select 标签的值?

如何使用 jQuery Validation Plugin 以编程方式判断表单是否有效

如何使用 jQuery 的 form.serialize 但排除空字段

小于 10 给数字加 0

使用 Chosen 插件更改 Select 中的 Select

使用 JQuery 判断组中是否没有选中单选按钮