简体   繁体   English

jQuery文件上传失败

[英]Jquery file upload fail

I found some code to make a jquery upload. 我找到了一些代码来进行jquery上传。 It almost works except I don't have any value in the $_FILES variable on the php file that handle the upload. 几乎可以正常工作,除非我在处理上传的php文件上的$ _FILES变量中没有任何值。 Here's the snippet 这是片段

<script type="text/javascript">
$(document).ready(function () {

    $("#formsubmit").click(function () {

        var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none" />');

        $("body").append(iframe);

        var form = $('#theuploadform');
        form.attr("action", "/upload.aspx");
        form.attr("method", "post");
        form.attr("enctype", "multipart/form-data");
        form.attr("encoding", "multipart/form-data");
        form.attr("target", "postiframe");
        form.attr("file", $('#userfile').val());
        form.submit();

        $("#postiframe").load(function () {
            iframeContents = $("#postiframe")[0].contentWindow.document.body.innerHTML;
            $("#textarea").html(iframeContents);
        });

        return false;

    });

});

</script>

<form id="theuploadform">
    <input id="userfile" name="userfile" size="50" type="file" />
    <input id="formsubmit" type="submit" value="Send File" />
</form>

<div id="textarea">
</div>

Does anyone know what's going on ? 有人知道发生了什么吗?

this is a header sent problem ! 这是标题发送的问题! to send file upload to server, you should to use FormData class in javascript/jQuery ! 要将文件上传发送到服务器,您应该在javascript / jQuery中使用FormData类! I suggest use this jquery plugin to make it easy : 我建议使用此jquery插件使其变得容易:

jQuery ajax form jQuery ajax表单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM