Search code examples
imageasp.net-mvc-3razoruploadify

MVC3 uploadify uploading images to folders


I am using uploadify for uploading multiple images with progress in ASP.NET MVC3 and I want to upload lets say from url Gallery/Upload/2 to folder with name 2 and Gallery/Upload/3 to folder 3 and so on. I just don´t know how. For get working uploading I used this sample and modified sample upload script to this:

public string Upload(HttpPostedFileBase fileData)
        {
            var id = 2;
            var fotka = new Photo();
            fotka.GalleryId = id;
            fotka.Description = fileData.FileName;
            fotka.Name = fileData.FileName;
            db.Photos.Add(fotka);
            db.SaveChanges();
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileData.FileName);
            var fileName = Path.GetFileName(fileData.FileName);
            string path = Server.MapPath("~/Fotky/") + id.ToString() + "\\" + fileNameWithoutExtension;
            // var path = Path.Combine(Server.MapPath("~/Fotky/"), galleryId.ToString(), "/", fileNameWithoutExtension);
            using (var input = new Bitmap(fileData.InputStream))
            {
                int width;
                int height;
                if (input.Width > input.Height)
                {
                    width = 128;
                    height = 128 * input.Height / input.Width;
                }
                else
                {
                    height = 128;
                    width = 128 * input.Width / input.Height;
                }
                using (var thumb = new Bitmap(width, height))
                using (var graphic = Graphics.FromImage(thumb))
                {
                    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

                    graphic.DrawImage(input, 0, 0, width, height);
                    System.IO.Directory.CreateDirectory(Server.MapPath("~/Fotky/") + id.ToString());
                    using (var output = System.IO.File.Create(path + "_small" + Path.GetExtension(fileName)))
                    {
                        thumb.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
            }
            fileData.SaveAs(path + Path.GetExtension(fileName));

            return "ok";
        }

as you can see I am using id as 2 as default, but I want to get it from parameters, is it posible? And how? Thank you

No one knows answer? It´s really important for me


Solution

  • You could have your controller action take the id as action argument:

    public string Upload(HttpPostedFileBase fileData, string id)
    {
        ...
    }
    

    and then pass this id when configuring your plugin:

    $("#fileuploader").fileUpload({
        'uploader': '@Url.Content("~/Scripts/uploader.swf")',
        'cancelImg': '@Url.Content("~/Images/cancel.png")',
        'buttonText': 'Select Image',
        'script': '@Url.Action("Upload", "Home", new { id = "2" })',
        'folder': '@Url.Content("~/uploads")',
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
        'multi': true,
        'auto': true
    });