-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' –