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是我有警報消息顯示。
問題: 我不能夠得到response.Width和response.Height。
感謝正確答案。我有一個混淆兩個寫下面的語句'string width =「0」; string height =「0」; Response.Write(@「{」「Width」「:」「0」「,」「Height」「:」「0」「};」); //我想用寬度和高度變量的上面一行 Response.Write(@「{Width:」+ width +「,Height:」+ height +「};」);' – imdadhusen
@imdadhusen:不知道我是否理解你的問題。我已經用一些關於json序列化的信息更新了我的答案。對我來說這是最好的方法。 – LeftyX
感謝您的寶貴意見!如果我有問題,我會通知你。 – imdadhusen