1
我想要將圖像上傳到我的服務器文件夾並將其調整大小後,從垃圾文件夾中刪除圖像。但後來我收到這條消息,它不讓我從垃圾文件夾中刪除圖像。我該如何解決這個問題?該進程無法訪問該文件,因爲它正在被另一個進程asp.net c#使用?
if (FileUpload1.HasFile)
{
long fileSize = FileUpload1.FileContent.Length;
double sizeinBytes = fileSize * 0.001;
FileUpload1.SaveAs(Server.MapPath("~/junk/" + FileUpload1.FileName));
string filepath = Server.MapPath("~/junk/" +FileUpload1.FileName);
System.IO.FileStream fs = System.IO.File.OpenRead(filepath);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
Bitmap resizedimage = ResizeImage(image, 500, 500);
resizedimage.Save(Server.MapPath("~/images/" + FileUpload1.FileName + ".jpeg"));
Image1.ImageUrl = "~/images/" + FileUpload1.FileName;
var filePath = Server.MapPath("~/junk/" + FileUpload1.FileName);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
關閉和處置刪除操作前,您的FileStream(FS)。或者使用「使用」語句。 – Manoj
@Manoj - 謝謝。這解決了問題 –