首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用支持XMLHttpRequest的AJAX文件上传,即9

使用支持XMLHttpRequest的AJAX文件上传,即9
EN

Stack Overflow用户
提问于 2014-05-22 14:44:29
回答 1查看 3.7K关注 0票数 2

我正在尝试使用ajax上传文件。下面的代码在所有浏览器上都能很好地工作,除了9和以前的版本。不幸的是,我不得不支持这些浏览器,所以我想知道如何修改这段代码,这样它就可以在即

我看到一些帖子建议使用iframe,但我看不出这是如何解决我的问题的。

我尝试使用fileInput.name,因为i.e似乎不允许我有一个文件数组,这意味着我实际上可以到达它发送的行,但我不确定该行应该是什么。xhr.send(fileInput);似乎不起作用。

我也尝试过使用formdata,但也发现ie9不支持它。

您的帮助将不胜感激。

代码语言:javascript
复制
 <script>
    function uploadFile(fileInput, label1, label2, filename) {
        var fileInput = document.getElementById(fileInput);

        var xhr;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } else {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xhr.open('POST', 'Create/Upload');
        xhr.setRequestHeader('Content-type', 'multipart/form-data');
        //Appending file information in Http headers

        //xhr.setRequestHeader('X-File-Name', filename);
        xhr.setRequestHeader('X-File-Type', fileInput.files[0].name);
        xhr.setRequestHeader('X-File-Type', fileInput.files[0].type);
        xhr.setRequestHeader('X-File-Size', fileInput.files[0].size);
        xhr.setRequestHeader('X-Type', label1);
        //Sending file in XMLHttpRequest
        xhr.send(fileInput.files[0]);

        //xhr.send(fileInput);


        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                $('#' + label1).text(xhr.responseText.replace(/\"/g, ""));
                document.getElementById(label1).style.color = "green";
                document.getElementById(label2).style.display = 'none';
            }
            else {
                $('#' + label1).text("File upload failed");
                document.getElementById(label1).style.color = "red";
            }
        }
    }
    document.getElementById('uploaderAuto').onsubmit = function () {

        myfile = $('#fileInputAuto').val();
        var ext = myfile.split('.').pop();
        ext = ext.toLowerCase();
        if (ext == "pdf" || ext == "docx" || ext == "doc" || ext == "odf" || ext == "rtf") {
            uploadFile('fileInputAuto', 'Auto', "AutoView", myfile);
        } else {
            alert("The following is a list of accepted file types:\n\n - Word Document (*.doc)\n - Word Document (*.docx)\n - Portable Document Format (*.pdf)\n - Open Document Format (*.odf)\n - Rich Text Format (*.rtf)\n\nPlease choose a file with one of these file types.");
        }

        return false;
    }

    document.getElementById('uploaderOther1').onsubmit = function () {
        myfile = $('#fileInputOther1').val();
        uploadFile('fileInputOther1', 'Other1', 'Other1View', myfile);
        return false;
    }

    document.getElementById('uploaderOther2').onsubmit = function () {
        myfile = $('#fileInputOther2').val();
        uploadFile('fileInputOther2', 'Other2', 'Other2View', myfile);
        return false;
    }
</script>
EN

回答 1

Stack Overflow用户

发布于 2014-05-23 09:43:15

我最终使用了这里的脚本:http://www.phpletter.com/Our-Projects/AjaxFileUpload/,它工作得很好。

我使用的是asp.net服务器端,因此本教程对我有所帮助:http://totalpict.com/b/asp.net%20generic/5/34396

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23799589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档