-1
我使用dropzone.js將多個圖像上傳到.NET MVC網站。當圖像進入服務器時,我調整它們大小,將信息保存到數據庫,然後將實際圖像保存在服務器上的文件夾中。當我上傳到一次一個工作的,但如果我同時做多個上傳,我得到這個錯誤每隔几上傳:如何在ASP.NET MVC網站中處理多個圖像上傳?
A generic error occurred in GDI+
谷歌搜索這個似乎這是一個通用的錯誤,當您無法保存作爲其他進程的文件正在使用該位置。我假設多個上傳文件試圖同時保存在同一個文件夾中,並且一個文件死掉了。它們不是相同的文件名。
我該如何避免這種情況?有沒有辦法讓線程在嘗試保存到同一文件夾之前等待一個完成?我想知道是否有一種方法就像是等待直到它可以保存的等待呼叫。
編輯更多的代碼
我不能複製我的整個功能,因爲它是很長等,但可以節省部分如下:
//Now we will try saving the actual file. Generate the file name.
String savedFileName = PhotoTools.GenerateImageFileName(photo.image_base_file_name);
String thumbnailSavedFileName = PhotoTools.GenerateImageFileName(photo.image_base_file_name, true);
//Store the file path (directory) that we are going to save the image to.
String directoryToSave = Server.MapPath(Constants.ImageDirectory + $"/{house_id}");
//Create the directory. This function will not do anything if it already exists.
Directory.CreateDirectory(directoryToSave);
//Save the images to the filesystem.
mainImage.Save(Path.Combine(directoryToSave, savedFileName), ImageFormat.Jpeg);
thumbnailImage.Save(Path.Combine(directoryToSave, thumbnailSavedFileName), ImageFormat.Jpeg);
他們不應該是相同的文件名文件名是這樣創建的:
/// <summary>
/// Generates the base property image filename based on the passed in information.
/// </summary>
/// <param name="propName">The house name that we will use for the file name.</param>
/// <returns>The generated file name.</returns>
public static String GenerateBaseImageFileName(String propName)
{
//Save the current datetime to create the filename with.
DateTime now = DateTime.Now;
//Generate the name.
return $"{CommonTools.RemoveSpecialCharacters(propName).Replace(" ","-")}-{now.Hour}{now.Second}{now.Millisecond}";
}
做這些影像被保存在同一文件夾具有相同的文件名?您將不得不提供一些代碼來陪同您的問題關於您如何保存這些數據。 –
@DanielShillcock添加了一些代碼。 – SolidSnake4444
嘗試對文件I/O操作的函數/代碼使用'lock' –