C# 如何在ASP.NET MVC网站中处理多个图像上载?

C# 如何在ASP.NET MVC网站中处理多个图像上载?,c#,asp.net-mvc,image-uploading,dropzone.js,C#,Asp.net Mvc,Image Uploading,Dropzone.js,我正在使用dropzone.js将多个图像上载到.NET MVC网站。当图像进入服务器时,我调整它们的大小,将信息保存到数据库中,然后将服务器上的实际图像保存到文件夹中。当我一次上传一个时,这是可行的,但如果我一次上传多个,每隔几次上传就会出现以下错误: A generic error occurred in GDI+ 谷歌搜索这似乎是一个通用错误,当其他进程正在使用该位置时,无法保存文件。我假设多个上传试图同时保存在同一个文件夹中,其中一个会死掉。它们不是相同的文件名 我怎样才能避免这种情况

我正在使用dropzone.js将多个图像上载到.NET MVC网站。当图像进入服务器时,我调整它们的大小,将信息保存到数据库中,然后将服务器上的实际图像保存到文件夹中。当我一次上传一个时,这是可行的,但如果我一次上传多个,每隔几次上传就会出现以下错误:

A generic error occurred in GDI+
谷歌搜索这似乎是一个通用错误,当其他进程正在使用该位置时,无法保存文件。我假设多个上传试图同时保存在同一个文件夹中,其中一个会死掉。它们不是相同的文件名

我怎样才能避免这种情况?有没有办法让线程等待一个线程完成,然后再尝试保存到同一文件夹中?我想知道是否有一种方法像wait调用那样等待,直到它可以保存

编辑更多代码

我无法复制我的整个功能,因为它很长,等等,但保存的部分如下:

//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}";
}
//
///根据传入的信息生成基本属性图像文件名。
/// 
///我们将用作文件名的房屋名称。
///生成的文件名。
公共静态字符串GenerateBaiseImageFileName(字符串propName)
{
//保存当前日期时间以创建文件名。
DateTime now=DateTime.now;
//生成名称。
返回$“{CommonTools.RemoveSpecialCharacters(propName).Replace(“,“-”)}-{now.Hour}{now.Second}{now.millisond}”;
}

通常此错误GDI+中发生的一般错误
表示文件或路径不存在:

因此,请尝试检查目录是否存在,如果不存在,请创建它。。然后再次检查是否已经有同名文件或完整路径是否存在:

尝试:


var guid=guid.New().ToString()//保存在同一文件夹中的这些图像是否具有相同的文件名?您必须提供一些代码来回答有关如何保存此数据的问题。@Danielshillock添加了一些代码。请尝试将
lock
与文件I/O一起使用到函数/代码中operations@YvetteColomb我想可能会有人打扰。关于我如何处理连接和线程…不确定。Dropzone自行处理将所有文件发送给我的问题,我在问题中没有显示的所有代码都是标准c#的业务逻辑代码。我不做任何关于线程或类似的事情。
var guid = Guid.New().ToString(); //<-- generate new random guid

//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.
if(!Directory.Exist(directoryToSave)
Directory.CreateDirectory(directoryToSave);

//Save the images to the filesystem.
mainImage.Save(Path.Combine(directoryToSave, savedFileName + "_" + guid), ImageFormat.Jpeg);