1

我在我的应用程序中获得了图像的拖放功能:
似乎HttpPostedFileBase使用以下代码将文件另存为:

public ActionResult SaveUploadedFile()
{
    bool isSavedSuccessfully = true;

    foreach (string fileName in Request.Files)
    {
        HttpPostedFileBase file = Request.Files[fileName];
    }
}    

我的最终目标是将图像路径存储在我的数据库中,但现在如果我可以将此HttpPostedFileBase-file 转换为 jpg 并将其存储在项目中的图像文件夹中,我会很高兴。关于如何解决这个问题的任何提示?

编辑:此代码似乎将 HttpPostedFileBase 转换为图像:

   public ActionResult SaveUploadedFile()
{
    bool isSavedSuccessfully = true;

    foreach (string fileName in Request.Files)
    {
        HttpPostedFileBase file = Request.Files[fileName];
         var filename = Path.GetFileName(file.FileName);

            var sourceimage = System.Drawing.Image.FromStream(file.InputStream);
            img = sourceimage;
    }
}  

我还添加了一个道具来存储它:

public Image img { get; set; }

编辑:

   public HttpPostedFileBase DropImg { get; set; }
   public Image img { get; set; }

 public ActionResult SaveUploadedFile(string test)
        {
            bool isSavedSuccessfully = true;

            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];
                DropImg = file;
                var filename = Path.GetFileName(file.FileName);

                var sourceimage = System.Drawing.Image.FromStream(file.InputStream);
                img = sourceimage;

                var fullPath = "~/Content/images/drops/" + file.FileName;
                file.SaveAs(Server.MapPath(fullPath));
            }




            if (isSavedSuccessfully)
            {
                return Json(new { Message = "File saved" });

            }
            else
            {
                return Json(new { Message = "Error in saving file" });
            }


        }

我希望这会将我的图像保存到文件夹中……我错过了什么吗?

4

1 回答 1

1
var fullPath = "~/UploadedImages/" + file.FileName;
file.SaveAs(Server.MapPath(fullPath));
于 2014-06-15T12:37:58.173 回答