【问题标题】:MVC3 uploadify uploading images to foldersMVC3 uploadify 将图像上传到文件夹
【发布时间】:2011-09-14 19:43:04
【问题描述】:

我正在使用 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,但我想从参数中获取它,这可能吗?如何?谢谢

没有人知道答案吗?这对我来说真的很重要

【问题讨论】:

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


    【解决方案1】:

    您可以让您的控制器操作将 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
    });
    

    【讨论】:

    • 谢谢。这是一个很好的解决方案。过去我已经混了好一阵子了。现在我可以充分利用 Uploadify。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2015-04-24
    • 2014-10-24
    相关资源
    最近更新 更多