开发者

How to parse JsonResult with Jquery AjaxUpload

the following code for upload image

 <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++;
                        });
                    }
                });
            });
        });

Server code: (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 + "}");
    }

and the return JsonResult is i have display in Alert message.

How to parse JsonResult with Jquery AjaxUpload

Problem: I am not able to get response.Width and response.Height.


first of all I would suggest to clean the HTML of your saveupload.aspx. You do not need it and it pollutes your response. You just need:

<%@ 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">

Another thing; when you get the response back in your script you can use parseJSON, like this:

var obj = jQuery.parseJSON(response);

now you should be able to access Width and Height:

obj.Width 

Last thing. Valums' Ajax Uploader has been replaced by the author with a new component. You can find it here It's very much similar but he's still updating this project so you might consider to switch.

UPDATE:

Another thing I would suggest is to use the jSon serializer (System.Web.Script.Serialization) to serialize the stream you want to return:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜