MVC3 uploadify将图像上传到文件夹

时间:2011-09-14 19:43:04

标签: image asp.net-mvc-3 razor uploadify

我正在使用uploadify上传多个图像并在ASP.NET MVC3中取得进展,我想从url Gallery / Upload / 2上传到名为2的文件夹和Gallery / Upload / 3到文件夹3,依此类推。我不知道怎么做。为了上传工作,我使用了这个sample并修改了示例上传脚本:

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";
        }

你可以看到我使用id作为默认值为2,但我想从参数中获取它,它是否可行?如何?谢谢

没人知道答案?这对我来说非常重要

1 个答案:

答案 0 :(得分:4)

您可以让控制器操作将id作为操作参数:

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

然后在配置插件时传递此ID:

$("#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
});