how will been jason output when we use ajax asynchronous upload?

Web Tips Q & ACategory: Krajee Pluginshow will been jason output when we use ajax asynchronous upload?
Mohsen17 asked 8 months ago

I want to use file input Plugin in asp.net web form. I prepare a web method and sent Json to this. the file was sent

and saved in my application properly. but I have problem when i want to send response from server to my asp.net

web page.

[WebMethod]

    public string UploadPersonal()

    {

        string returnTxt = “your image was saved successfuly”;

        string filepath = Server.MapPath(“~/PersonalImages”);

        HttpFileCollection uploadedFiles = HttpContext.Current.Request.Files;

        if (uploadedFiles.Count == 0)

        {

            return returnTxt = “please select your image”;

        }
        else

        {

            for (int i = 0; i < uploadedFiles.Count; i++)

            {
                HttpPostedFile userPostedFile = uploadedFiles[i];

                string filename = userPostedFile.FileName;

                string fileExt = System.IO.Path.GetExtension(filename);

                try

                {
                    if (fileExt.ToLower() == “.jpg” || fileExt.ToLower() == “.png” || fileExt.ToLower() == “.jepg” ||

fileExt.ToLower() == “.tif”)

                    {
                        int filesize = userPostedFile.ContentLength;

                        if (filesize <= 8390000 && filesize > 0)

                        {

                            filename = “MyImage” + fileExt;

                            userPostedFile.SaveAs(filepath + “\\” + Path.GetFileName(filename));

                            AdminAccess.UpdateSeller(“MyImage”, null, null, null, null, null, null, null, null, filename);

                        }

                        else

                        {
                            returnTxt = “maximum allowed size is 1 MB”;

                        }
                    }
                    else

                    {
                        returnTxt = “please select correct format”;

                    }

                }
                catch

                {
                    returnTxt = “other error”;

                }
            }
        }

        object retlist = JSONHelper.ToJSON(returnTxt);

        return returnTxt;

    }

I studied your demo example codes which were prepared according to PHP.
Like this one

    <?php

    // …
    // SERVER CODE that processes ajax upload and returns a JSON response. Your server action
    // must return a json object containing initialPreview, initialPreviewConfig, & append.
    // An example for PHP Server code is mentioned below.
    // …

    $p1 = $p2 = [];

    if (empty($_FILES[‘kartik-input-704’][‘name’])) {
        echo ‘{}’;
    return;
    }

    for ($i = 0; $i < count($_FILES[‘kartik-input-704’][‘name’]); $i++) {

        $j = $i + 1;

        $key = ‘<code to parse your image key>’;

        $url = ‘<your server action to delete the file>’;

        $p1[$i] = “<img style=’height:160px’ src=’http://path.to.uploaded.file/{$key}.jpg’ class=’file-preview-image’>”,;

        $p2[$i] = [‘caption’ => “Animal-{$j}.jpg”, ‘width’ => ‘120px’, ‘url’ => $url, ‘key’ => $key];

    }

    echo json_encode([

    ‘initialPreview’ => $p1,

    ‘initialPreviewConfig’ => $p2,

    ‘append’ => true // whether to append these configurations to initialPreview.

    // if set to false it will overwrite initial preview
    // if set to true it will append to initial preview
    // if this propery not set or passed, it will default to true.
    ]);

    ?>

but how will be Json output from above  PHP code? because I want to prepare asp.net codes according to

needed outputs

Login or register to contribute or comment.

Login   Register