问题:首先,我的“创建”控制器操作方法以两种不同的方式创建两个文件。但是,我的程序无法删除使用file.SaveAs(path)创建的文件; 但是,我可以成功删除使用创建的其他文件 imgPhoto.Save(smallImageFilePath,System.Drawing.Imaging.ImageFormat.Jpeg);
以下是我的Create控制器操作方法的HttpPost重载,包括一个ScaleByPercent方法调用:(在底部粘贴完整的错误消息)
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Create(HttpPostedFileBase file, Models.Gallery gallerycm)
{
ViewBag.Message = "Testing Gallery File Create";
if (file != null && file.ContentLength > 0)
try
{
string path = Path.Combine(Server.MapPath("~/Images/demo/gallery"),
Path.GetFileName(file.FileName));
//System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal);
//System.Drawing.Image MainImgPhotoVert = System.Drawing.Image.FromFile(path);
/*
System.Drawing.Image MainImgPhotoVert = System.Drawing.Image.FromStream(System.IO.Stream file);
Bitmap MainImgPhoto = (System.Drawing.Bitmap)ScaleByPercent(MainImgPhotoVert, 100);
MainImgPhoto.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
MainImgPhoto.Dispose();
*/
file.SaveAs(path);
file.InputStream.Flush(); //useless
file.InputStream.Close(); //less than useless
file.InputStream.Dispose(); //complete waste of keystrokes
//System.IO.File.SetAttributes(path, System.IO.FileAttributes.Normal);
// Validating whether the following commented code releases a recently created
// file from IIS for file Delete. Problem occuring in the Visual Studio test environment.
//file.InputStream.Dispose();
//GC.Collect();
//GC.WaitForPendingFinalizers();
// Create the Thumbnail image
string smallImageFilePath = Path.Combine(Server.MapPath("~/Images/demo/gallery/") + "ThumbSize" + (file.FileName));
//allocate an Image object from the uploaded full sized .jpg
System.Drawing.Image imgPhotoVert = System.Drawing.Image.FromFile(path);
Bitmap imgPhoto = (System.Drawing.Bitmap)ScaleByPercent(imgPhotoVert, 50);
imgPhoto.Save(smallImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
var gallery = new Gallery();
//gallery.PhotoNumberID = 9;
gallery.Filename = file.FileName;
if (gallerycm.PhotoDescription == null)
gallerycm.PhotoDescription = " ";
gallery.PhotoDescription = gallerycm.PhotoDescription;
var galleryContext = new EFDbGalleryContext();
galleryContext.Gallery.Add(gallery);
galleryContext.SaveChanges();
}
catch (Exception ex)
{
TempData["SomeData"] = file.FileName + " Upload exception. The Details follow: " + ex.ToString();
return RedirectToAction("Index");
}
else
{
ViewBag.Message = "You have not specified a file.";
}
TempData["SomeData"] = "Photo was successfully Added";
return RedirectToAction("Index");
}
static System.Drawing.Image ScaleByPercent(System.Drawing.Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent / 100);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
}
以下是我的删除控制器操作的摘录: //从FileSystem中删除小文件和大文件 System.IO.File.Delete(smallImageFilePath);
//System.IO.File.GetAccessControl(largeImageFilePath);
try
{
System.IO.File.GetAccessControl(largeImageFilePath); // Does not help
System.IO.File.Delete(largeImageFilePath);
}
catch (System.IO.IOException e)
{
TempData["SomeData"] = " Delete exception. The Details follow: " + e.ToString();
return RedirectToAction("Index");
}
另请注意: 如果我在VS Debug中暂停程序,我可以在Windows资源管理器中删除位图文件,但另一个返回:
“正在使用的文件” “无法完成操作,因为文件在IIS工作进程中处于打开状态。关闭文件并重试。” 如果我关闭VS并返回我可以删除它,但是当我在Godaddy服务器上部署所有内容时,这对我没有帮助。
以下是总消息: 删除例外。详细信息如下:System.IO.IOException:进程无法访问文件'C:\ aspnet4_cs \ Pettigoats \ Pettigoats \ Images \ demo \ gallery \ WalkingOnPorch.jpg',因为它正被另一个进程使用。 at System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)at System.IO.File.InternalDelete(String path,Boolean checkHost)at System.IO.File.Delete(String path)at Pettigoats.Controllers.CMAdminController.Delete (int32 id)在c:\ aspnet4_cs \ Pettigoats \ Pettigoats \ Controllers \ CMAdminController.cs:第53行