2011-06-27 29 views
0

上傳圖片下面的代碼如何解析JsonResult使用jQuery AjaxUpload

<a id="addImage" href="javascript:;">Add Image</a> 

的Javascript:

$().ready(function() { 
      var counter = 0; 
      $(function() { 
       var btnUpload = $('#addImage'); 
       new AjaxUpload(btnUpload, { 
        action: 'saveupload.aspx', 
        name: 'uploadimage', 
        dataType: 'json', 
        onSubmit: function (file, ext) { 
         $("#loading").show(); 
        }, 
        onComplete: function (file, response) { 
         alert(response); 
         var uploadedfile = "UserData/" + file; 
         $("#uploadImageWrapper").append(" 
      <div class='imageContainer offset' id='current" + counter + "'> 
      <img height='65px' width='65px' src='" + uploadedfile + "' alt='" + uploadedfile + "'/></div>"); 
         $('#current' + counter).fadeIn('slow', function() { 
          $("#loading").hide(); 
          $("#message").show(); 
          $("#message").html("Added successfully!"); 
          $("#message").fadeOut(3000); 
          counter++; 
         }); 
        } 
       }); 
      }); 
     }); 

服務器代碼:(saveupload.aspx.cs)

protected void Page_Load(object sender, EventArgs e) 
    { 
     HttpFileCollection uploadedFiles = Request.Files; 
     int i = 0; 
     string width = "0"; 
     string height = "0"; 
     if (uploadedFiles.Count > 0) 
     { 
      while (!(i == uploadedFiles.Count)) 
      { 
       HttpPostedFile userPostedFile = uploadedFiles[i]; 
       if (userPostedFile.ContentLength > 0) 
       { 
        string filename = userPostedFile.FileName.Substring(userPostedFile.FileName.LastIndexOf("\\") + 1); 
        userPostedFile.SaveAs(Path.Combine(Server.MapPath("UserData"), filename)); 
        Bitmap img = new Bitmap(Path.Combine(Server.MapPath("UserData"), filename)); 
        width = img.Width.ToString(); 
        height = img.Height.ToString(); 
       } 
       i += 1; 
      } 
     } 
    //I would like to return Uploaded image Height and Width 
     Response.Write(@"{Width:" + width + ", Height:" + height + "}"); 
    } 

並返回JsonResult是我有警報消息顯示。 enter image description here

問題: 我不能夠得到response.Width和response.Height。

回答

1

首先我建議清理你的的HTML saveupload.aspx。你不需要它,它會污染你的迴應。 您只需要:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="saveupload.aspx.cs" Inherits="WebApplication1.saveupload" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

另一件事;當你在腳本中得到響應時,可以使用parseJSON,如下所示:

var obj = jQuery.parseJSON(response);

現在你應該能夠訪問寬度和高度:

obj.Width 

最後一件事。 Valums的Ajax Uploader已被作者用一個新組件取代。 你可以找到它here 這是非常相似,但他仍然在更新這個項目,所以你可能會考慮切換。

UPDATE:

另一件事,我建議是使用JSON序列(System.Web.Script.Serialization)序列化要返回流:

var jSon = new JavaScriptSerializer(); 
var OutPut = jSon.Serialize(myClass); 
Response.Write(OutPut); 
+0

感謝正確答案。我有一個混淆兩個寫下面的語句'string width =「0」; string height =「0」; Response.Write(@「{」「Width」「:」「0」「,」「Height」「:」「0」「};」); //我想用寬度和高度變量的上面一行 Response.Write(@「{Width:」+ width +「,Height:」+ height +「};」);' – imdadhusen

+1

@imdadhusen:不知道我是否理解你的問題。我已經用一些關於json序列化的信息更新了我的答案。對我來說這是最好的方法。 – LeftyX

+0

感謝您的寶貴意見!如果我有問題,我會通知你。 – imdadhusen