Fork me on GitHub

Ajaxfileupload上传文件

下载:http://www.phpletter.com/download_project_version.php

就是利用Iframe ,使用什么没有好说的

HTML

<script src="AjaxFileUploaderV2.1/jquery.js" type="text/javascript"></script>
    <script src="AjaxFileUploaderV2.1/ajaxfileupload.js" type="text/javascript"></script>
    <script type="text/javascript">
        function ajaxFileUpload() {
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });

            $.ajaxFileUpload({
                url: 'Handler1.ashx',
                secureuri: false,
                fileElementId: 'file',
                dataType: 'json',
                data: { name: 'Irving', id: '1991428' },
                success: function (data, status) {
                    alert(data.msg);
                },
                error: function (data, status,e) {
                    alert(e);
                }
            }
        )
            return false;
        }
    </script>
</head>
<body>
    <img id="loading" src="AjaxFileUploaderV2.1/loading.gif" style="display: none;" />
    <form method="post" enctype="multipart/form-data">
        <input id="file" type="file" size="45" name="fileToUpload" >
        <button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">
            Upload</button>
    </form>
</body>

hander测试下

            context.Response.ContentType = "text/html";
            HttpPostedFile file = context.Request.Files["fileToUpload"];
            if (file != null && string.IsNullOrEmpty(file.FileName) == false)
            {
                file.SaveAs(context.Server.MapPath("~/Files/") + file.FileName);
            }
            context.Response.Write("{status:'200',success:true,msg:'" + "OK" + "'}");

注意问题

1.返回JSON格式(http://bbs.csdn.net/topics/390050342),context.Response.ContentType = "text/html"; ,否则浏览器总是提示将返回的JSON结果另存为文件

2.jQuery.handleError(s, xml, status, e); jquery的handleError函数,但此函数在1.4.2之后不存在了,所以要不修改插件源码,或者用低版本了啦

3.IE9兼容性问题 找不到链接了

4.ajaxToolkit http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

 

5.struts2 http://www.blogjava.net/sxyx2008/archive/2010/11/02/336826.html

posted @ 2013-03-01 18:35  花儿笑弯了腰  阅读(790)  评论(0编辑  收藏  举报