Ajax文件上传(HTML5)

时间:2014-02-12 14:02:18

标签: javascript jquery ajax html5

是否可以使用HTML5 + Ajax + jQuery + C#上传(pdf)文件?

我需要上传一个没有重新加载页面的文件。

由于我不是网络开发人员,请保持简单'回答这个问题。:)

2 个答案:

答案 0 :(得分:0)

使用 jquery.form 插件,可以非常简单地执行您想要的操作:

<强> HTML:

<form id="form" action="upload_page.php" method="post" enctype="multipart/form-data">
     <input type="file" size="60" name="file">
     <input type="submit" value="Upload file">
</form>

<div id="progress">
     <div id="bar"></div>
     <div id="percent">0%</div >
</div>

<强> JS:

$(document).ready(function () {

    var options = {
        beforeSend: function () {
            $("#progress").show();
            //clear everything
            $("#bar").width('0%');
            $("#message").html("");
            $("#percent").html("0%");
        },
        uploadProgress: function (event, position, total, percentComplete) {
            $("#bar").width(percentComplete + '%');
            $("#percent").html(percentComplete + '%');

        },
        success: function () {
            $("#bar").width('100%');
            $("#percent").html('100%');

        },
        complete: function (response) {
            $("#message").html("<font color='green'>" + response.responseText + "</font>");
        },
        error: function () {
            $("#message").html("<font color='red'> ERROR: unable to upload files</font>");

        }

    };

    $("#form").ajaxForm(options);

});

这也会为您提供一个很好的进度条,您可以根据需要设置样式。

Plugin page

答案 1 :(得分:0)

我们在GPL包中用c#+ ajax编写了一个ASP.NET上传器,你可以在这里找到:

https://code.google.com/p/flexpaper/downloads/list