ajaxFileUpload插件的使用 ------------------插件

這個插件只能上傳,不能判斷文件大小,類型,要自己寫js判斷,或後臺判斷

 

 

http://www.phpletter.com/Our-Projects/AjaxFileUpload/

 

可以直接對DEMO進行修改就行

 

下面是一個函數:

 

//ajax上傳頭像圖片
function ajaxFileUpload() {
    var filePath = $("#fileToUpload").val();
    if (!filePath || filePath == "" || filePath == null) {
        $("#fileToUploadMsg").html("").html("<font color='Red'>請選擇一個上傳的圖片</font>");
        return false;
    }
    //顯示和隱藏加載圖片
    $("#loading").ajaxStart(function () {
        $(this).show();
    })

    .ajaxComplete(function () {
        $(this).hide();
    });

    //ajax上傳頭像圖片
    $.ajaxFileUpload
  (
   {
       url: '/User/Upload',
       secureuri: false,
       fileElementId: 'fileToUpload',
       dataType: 'json',//這個是後臺返回的數據類型,可以是json , text, html , script
       success: function (data, status) {
           var error = data.err;
           var filepath = data.msg;
           if (error != "") {
               asyncbox.tips(error, 'error');//異步盒子插件的提示
           } else {
               $("#viewImage").attr("src", filepath);
               $("#settingLeftImage").attr("src", filepath);
               asyncbox.tips('上傳頭像成功 !', 'success');
           }
       },
       error: function (data, status, e) {
           asyncbox.tips(e, 'error');
       }
   }
  )
    return false;
}

 

 

下面是下載的一個文件,(別人寫的)

 

jQuery插件AjaxFileUpload可以實現ajax文件上傳

jQuery插件AjaxFileUpload可以實現ajax文件上傳,該插件使用非常簡單,首先了解一下正確使用AjaxFileUpload插件的方法,然後再瞭解一些常見的錯誤信息和解決方法。

使用說明

需要使用jQuery庫文件 和AjaxFileUpload庫文件

使用實例

http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupload.js

一,包含文件部分

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="ajaxfileupload.js"></script>

二,HTML部分

    <img id="loading " src="loading.gif" style="display:none;">
    <input id="fileToUpload " type="file" size="20" name="fileToUpload " class="input">
    <button class="button" id="buttonUpload" οnclick="return ajaxFileUpload ();">上傳</button>

只需要三個元素,一個動態加載小圖標、一個文件域和一個按鈕
注意:使用AjaxFileUpload插件上傳文件可不需要form,如下:

    <form name="form" action="" method="POST" enctype="multipart/form-data">
    ……相關html代碼……
    </form>

因爲AjaxFileUpload插件會自動生成一個form提交表單。

對於file文件域ID和name,ajaxFileUpload插件fileElementId參數需要獲取文件域ID,如果處理上傳文件操作就需要知道文件域name,以便獲取上傳文件信息,這兩者關係一定要清楚。

三,javascript部分

    <script type="text/javascript">
    function ajaxFileUpload (){
    loading();//動態加載小圖標
    $.ajaxFileUpload ({
    url :'upload.php',
    secureuri :false,
    fileElementId :'fileToUpload',
    dataType : 'json',
    success : function (data, status){
    if(typeof(data.error) != 'undefined'){
    if(data.error != ''){
    alert(data.error);
    }else{
    alert(data.msg);
    }
    }
    },
    error: function (data, status, e){
    alert(e);
    }
    })
    return false;
    }
    function loading (){
    $("#loading ").ajaxStart(function(){
    $(this).show();
    }).ajaxComplete(function(){
    $(this).hide();
    });
    }
    </script>

主要參數說明:
1,url表示處理文件上傳操作的文件路徑,可以測試URL是否能在瀏覽器中直接訪問,如上:upload.php
2,fileElementId表示文件域ID,如上:fileToUpload
3,secureuri是否啓用安全提交,默認爲false
4,dataType數據數據,一般選json,javascript的原生態
5,success提交成功後處理函數
6,error提交失敗處理函數

上面有兩個方法,一個動態加載小圖標提示函數loading()和ajaxFileUpload文件上傳$.ajaxFileUpload()函數,這與我們使用jQuery.ajax()函數差不多,使用很簡單,
這裏我省略了PHP處理上傳文件,使用jQuery插件 AjaxFileUpload實現ajax文件就這麼簡單。

同時我們需要了解相關的錯誤提示

1,SyntaxError: missing ; before statement錯誤
如果出現這個錯誤就需要檢查url路徑是否可以訪問

2,SyntaxError: syntax error錯誤
如果出現這個錯誤就需要檢查處理提交操作的PHP文件是否存在語法錯誤

3,SyntaxError: invalid property id錯誤
如果出現這個錯誤就需要檢查屬性ID是否存在

4,SyntaxError: missing } in XML expression錯誤
如果出現這個錯誤就需要檢查文件域名稱是否一致或不存在

5,其它自定義錯誤
大家可使用變量$error直接打印的方法檢查各參數是否正確,比起上面這些無效的錯誤提示還是方便很多。

使用jQuery插件AjaxFileUpload實現無刷新上傳文件非常實用,由於其簡單易用,因些這個插件相比其它文件上傳插件使用人數最多,非常值得推薦。

 

處理頁面:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class web_ajax_FileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;


        //if (files[0].ContentLength > 5)
        //{
        //    Response.Write("{");
        //    Response.Write("msg:'" + files[0].FileName + "',");
        //    Response.Write("error:'文件上傳失敗'");
        //    Response.Write("}");
        //}
        //else
        //{
        //    Response.Write("{");
        //    Response.Write("msg:'沒有文件被上傳',");
        //    Response.Write("error:'文件上傳失敗'");
        //    Response.Write("}");
        //}
        files[0].SaveAs("d:/adw.jpg");
        Response.Write("{");
        Response.Write("msg:'a',");
        Response.Write("error:''");
        Response.Write("}");

        //Response.Write("{");
        //Response.Write("msg:'ggg\n',");
        //Response.Write("error:'aa\n'");
        //Response.Write("}");
        Response.End();
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章