如何在mvc中上传之前获取图像源的路径?

时间:2012-09-20 07:07:52

标签: c# asp.net-mvc image

我想从上传的位置获取图片的路径......

例如:我的图像位于E:驱动器和图像文件夹中,我希望在上传图像之前获取该路径

我的Controllercode位于

之下
    public ActionResult UploadImage(UploadImageModel model)
    {

        if (ModelState.IsValid)
        {

            Bitmap original = null;
            var name = "newimagefile";
            var errorField = string.Empty;

            if (model.IsUrl)
            {
                errorField = "Url";
                name = GetUrlFileName(model.Url);
                original = GetImageFromUrl(model.Url);
            }
            else if (model.IsFlickr)
            {
                errorField = "Flickr";
                name = GetUrlFileName(model.Flickr);
                original = GetImageFromUrl(model.Flickr);
            }
            else if (model.File != null) 
            {

                errorField = "File";
                name = Path.GetFileNameWithoutExtension(model.File.FileName);
                original = Bitmap.FromStream(model.File.InputStream) as Bitmap;
            }


            if (original != null)
            {
                var img = CreateImage(original, model.X, model.Y, model.Width, model.Height);


                var fn = Server.MapPath("~/Content/img/" + name + ".png");
                img.Save(fn, System.Drawing.Imaging.ImageFormat.Png);


                return RedirectToAction("Index");
            }
            else 
                ModelState.AddModelError(errorField, "Your upload did not seem valid. Please try again using only correct images!");
        }

        return View(model);
    }

1 个答案:

答案 0 :(得分:1)

这是不可能的。如果浏览器从客户端发送完整路径,则这是一种安全威胁。