圖片上傳的ajax代碼
① Jquery上傳圖片提交到一般處理程序中返回圖片路徑
data:null
傳遞給伺服器的數據為空,當然沒有接收到文件數據。
上傳文件時一般使用ajaxFileUpload方法。
② img如何接收jquery ajax非同步上傳的動態圖片
你的表述沒看懂,jquery 非同步上傳過後獲取路徑 直接給img 的src賦值不就完了?
③ jqueryajax不能上傳圖片
不能上傳的原因可能是jquery插件使用不正確。
解決方法:
1、在head之間加入jquery引用
<scripttype="text/javascript" src="jquery.js"></script>
<scriptsrc="project/js/jquery.form.js" type="text/javascript"></script>
<scriptsrc="project/js/fileload.js" type="text/javascript"></script>
2、定義fileload.js,代碼如下:
function createHtml(obj) {
  var htmstr = [];
  htmstr.push( "<form id='_fileForm' enctype='multipart/form-data'>");
  htmstr.push( "<table cellspacing="0" cellpadding="3" style="margin:0 auto; margin-top:20px;">");
  htmstr.push( "<tr>");
  htmstr.push( "<td class="tdt tdl">請選擇文件:</td>");
  htmstr.push( "<td class="tdt tdl"><input id="loadcontrol" type="file" name="filepath" id="filepath" /></td>");
  htmstr.push( "<td class="tdt tdl tdr"><input type="button" onclick="fileloadon()" value="上傳"/></td>");
  htmstr.push( "</tr>");
  htmstr.push( "<tr> <td class="tdt tdl tdr" colspan='3'style='text-align:center;'><div id="msg"> </div></td> </tr>");
  htmstr.push( "<tr> <td class="tdt tdl tdr" style=" vertical-align:middle;">圖片預覽:</td> <td class="tdt tdl tdr" colspan="2"><div style="text-align:center;"><img src="project/Images/NoPhoto.jpg"/></div></td> </tr>");
  htmstr.push( "</table>")
  htmstr.push( "</form>");
  obj.html(htmstr.join(""));
}
function fileloadon() {
  $("#msg").html("");  
  $("img").attr({ "src": "project/Images/processing.gif" });
  $("#_fileForm").submit(function () {  
    $("#_fileForm").ajaxSubmit({
      type: "post",
      url: "project/help.aspx",
      success: function (data1) {
      var remsg = data1.split("|");
      var name = remsg[1].split("/");
      if (remsg[0] == "1") {
      var type = name[4].substring(name[4].indexOf("."), name[4].length);
      $("#msg").html("文件名:" + name[name.length - 1] + "  --- " + remsg[2]);
      switch (type) {
        case ".jpg":
        case ".jpeg":
        case ".gif":
        case ".bmp":
        case ".png":
        $("img").attr({ "src": remsg[1] });
        break;
      default:
        $("img").attr({ "src": "project/Images/msg_ok.png" });
        break;
      }
      } else {
        $("#msg").html("文件上傳失敗:" + remsg[2]);
        $("img").attr({ "src": "project/Images/msg_error.png" });
      }
      },
      error: function (msg) {
        alert("文件上傳失敗");  
      }
    });
    return false;
  });
  $("#_fileForm").submit();
}
3、服務端處理上傳。
protected void Page_Load(object sender, EventArgs e)
  {
    try
    {
      HttpPostedFile postFile = Request.Files[0];
      //開始上傳
      string _savedFileResult = UpLoadFile(postFile);
      Response.Write(_savedFileResult);
    }
    catch(Exception ex)
    {
      Response.Write("0|error|上傳提交出錯");
    }
  }
  public string UpLoadFile(HttpPostedFile str)
  {
    return UpLoadFile(str, "/UpLoadFile/");
  }
  public string UpLoadFile(HttpPostedFile httpFile, string toFilePath)
  {
    try
    {
      //獲取要保存的文件信息
      string filerealname = httpFile.FileName;
      //獲得文件擴展名
      string fileNameExt = System.IO.Path.GetExtension(filerealname);
      if (CheckFileExt(fileNameExt))
      {
        //檢查保存的路徑 是否有/結尾
        if (toFilePath.EndsWith("/") == false) toFilePath = toFilePath + "/";
        //按日期歸類保存
        string datePath = DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd") + "/";
        if (true)
        {
          toFilePath += datePath;
        }
        //物理完整路徑          
        string toFileFullPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + toFilePath;
        //檢查是否有該路徑 沒有就創建
        if (!System.IO.Directory.Exists(toFileFullPath))
        {
          Directory.CreateDirectory(toFileFullPath);
        }
        //得到伺服器文件保存路徑
        string toFile = Server.MapPath("~" + toFilePath);
        string f_file = getName(filerealname);
        //將文件保存至伺服器
        httpFile.SaveAs(toFile + f_file);
        return "1|" + toFilePath + f_file + "|" + "文件上傳成功";
      }
      else
      {
        return "0|errorfile|" + "文件不合法";
      }
    }
    catch (Exception e)
    {
      return "0|errorfile|" + "文件上傳失敗,錯誤原因:" + e.Message;
    }
  }
  /// <summary>
  /// 獲取文件名
  /// </summary>
  /// <param name="fileNamePath"></param>
  /// <returns></returns>
  private string getName(string fileNamePath)
  {
    string[] name = fileNamePath.Split('\');
    return name[name.Length - 1];
  }
  /// <summary>
  /// 檢查是否為合法的上傳文件
  /// </summary>
  /// <param name="_fileExt"></param>
  /// <returns></returns>
  private bool CheckFileExt(string _fileExt)
  {
    string[] allowExt = new string[] { ".gif", ".jpg", ".jpeg", ".rar",".png" };
    for (int i = 0; i < allowExt.Length; i++)
    {
      if (allowExt[i] == _fileExt) { return true; }
    }
    return false;
  }
  public static string GetFileName()
  {
    Random rd = new Random();
    StringBuilder serial = new StringBuilder();
    serial.Append(DateTime.Now.ToString("HHmmss"));
    serial.Append(rd.Next(100, 999).ToString());
    return serial.ToString();
  }
4、運行defualt.aspx頁面以後顯示的效果是:

④ JS ajaxFileUpload 傳參數的時候 有沒有和ajax一樣的 type:'post' 的設置
語法:$.ajaxFileUpload([options])
options參數說明:
1、url  上傳處理程序地址。
2,fileElementId  需要上傳的文件域的ID,即<input type="file">的ID。
3,secureuri 是否啟用安全提交,默認為false。 
4,dataType 伺服器返回的數據類型。可以為xml,script,json,html。如果不填寫,jQuery會自動判斷。
5,success提交成功後自動執行的處理函數,參數data就是伺服器返回的數據。
6,error 提交失敗自動執行的處理函數。
7,data	 自定義參數。這個東西比較有用,當有數據是與上傳的圖片相關的時候,這個東西就要用到了。
8, type	  當要提交自定義參數時,這個參數要設置成post
⑤ PHP 用AJAX 做多文件上傳
比較推薦使用swfupload上傳代碼,它是把swf和javascript結合起來,做成上傳代碼。功能應該是當前最豐富的。
它可以實現純粹html、javascrip難以逾越的功能:
(1)可以同時上傳多個文件;
(2)類似AJAX的無刷新上傳;
(3)可以顯示上傳進度;
(4)良好的瀏覽器兼容性;
目前QQ空間和博客網站,比較先進的圖片上傳也是基於swf和js代碼結合的做法。
關於swfupload,你可以詳細去網路上看,不重復了。
http://ke..com/view/1332553.htm
