gsiradze
gsiradze

Reputation: 4733

return JSON from MVC equivalent to web forms

I'm trying to upload files using plupload and in MVC I have code like this:

public ActionResult Upload()
    {
        for (int i = 0; i < Request.Files.Count; i++)
        {
            var file = Request.Files[i];
            file.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "Uploads/" + file.FileName);
        }

        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }

and now I want to do same in web forms. I have added these code in my submit button click

 protected void submitBtn_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Request.Files.Count; i++)
        {
            var file = Request.Files[i];
            file.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "Uploads/" + file.FileName);
        }

    }

and I need to send the success=true to my plupload javascript code to write these photos in my folder.

 $(document).ready(function () {
        var uploader = new plupload.Uploader({
            runtimes: 'html5,flash,silverlight,html4',
            browse_button: 'pickfiles',
            container: document.getElementById('container'),
            url: '/Admin.aspx',
            flash_swf_url: '/Scripts/Moxie.swf',
            silverlight_xap_url: '/Scripts/Moxie.xap',

            filters: {
                max_file_size: '10mb',
                mime_types: [
                    { title: "Image files", extensions: "jpg,gif,png" },
                    { title: "Zip files", extensions: "zip" }
                ]
            },

            init: {
                PostInit: function () {
                    document.getElementById('uploadfiles').onclick = function () {
                        uploader.start();
                        return false;
                    };
                },


                UploadProgress: function (up, file) {
                    _file_name = file.name;

                    $('#PhotoBox').val(_file_name);

                    console.log(file.name);

                },

                Error: function (up, err) {
                    alert("\nError #" + err.code + ": " + err.message);
                }
            }
        });

        uploader.init();
    });

so Is there any equivalent of

 return Json(new { success = true }, JsonRequestBehavior.AllowGet);

for asp.net Web Forms?

Upvotes: 1

Views: 283

Answers (1)

user3297426
user3297426

Reputation:

simply put

if (!IsPostBack) {
    for (int i = 0; i < Request.Files.Count; i++)
    {
        var file = Request.Files[i];
        file.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "Uploads/" + file.FileName);
    }
}

in your page load event and everything will okay

Upvotes: 2

Related Questions