【发布时间】:2017-03-19 21:36:38
【问题描述】:
我有一个 asp.net mvc 应用程序,我可以在其中一次上传多张图像并且效果很好,但是我在从原始图像中创建缩略图图像时遇到了问题。
代码如下:
[HttpPost]
public ActionResult SaveUploadedFile()
{
bool isSavedSuccessfully = true;
string fName = "";
try
{
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
fName = file.FileName;
if (file != null && file.ContentLength > 0)
{
var originalDirectory = new DirectoryInfo(string.Format("{0}Images", Server.MapPath(@"\")));
string pathString = Path.Combine(originalDirectory.ToString(), "Gallery");
string pathString2 = Path.Combine(originalDirectory.ToString(), "Gallery\\Thumbs");
//var fileName1 = Path.GetFileName(file.FileName);
bool exists = Directory.Exists(pathString);
bool exists2 = Directory.Exists(pathString2);
if (!exists)
Directory.CreateDirectory(pathString);
if (!exists2)
Directory.CreateDirectory(pathString2);
var path = string.Format("{0}\\{1}", pathString, file.FileName);
file.SaveAs(path);
//WebImage img = new WebImage(file.InputStream);
WebImage img = new WebImage(fileName);
//if (img.Width > 1000)
img.Resize(100, 100);
img.Save(pathString2);
}
}
}
catch (Exception ex)
{
isSavedSuccessfully = false;
}
if (isSavedSuccessfully)
{
return Json(new { Message = fName });
}
else
{
return Json(new { Message = "Error in saving file" });
}
}
所以基本上file.SaveAs(path); 是最后一行有效,我实际上保存了图像,但在那一行之后我尝试创建和保存拇指但它不起作用(我确实得到了之前创建的Thumbs 文件夹) .
我也收到了Error in saving file 响应,但那是因为我正在尝试创建和保存拇指,如果我删除它,那么我不会得到它,而且我不知道如何将实际错误返回到实际看看错误是什么。
【问题讨论】:
-
可能是因为
img.Save(pathString2);中的pathString2具有值“Gallery\\Thumbs”,而您忘记将文件名添加到补丁。尝试在catch中设置断点,看看抛出了哪个异常。 -
它是空的。为什么它会有那个值而不是我在这里分配的完整路径 string pathString2 = Path.Combine(originalDirectory.ToString(), "Gallery\\Thumbs"); ?而且我不明白“忘记在补丁中添加文件名”,请举个例子。
-
pathstring2 有完整且正确的路径,我刚刚检查了一个断点。
-
我认为问题是我没有传递我应该传递给
WebImage img的内容,因为断点在该行之后没有捕获。
标签: c# asp.net-mvc thumbnails image-uploading