【发布时间】:2018-01-29 17:00:29
【问题描述】:
我正在使用 dropzone.js 将多个图像上传到 .NET MVC 网站。当图像进入服务器时,我调整它们的大小,将信息保存到数据库中,然后将服务器上的实际图像保存在一个文件夹中。当我一次上传一个时,这是可行的,但是如果我一次上传多个,每隔几次上传就会出现此错误:
A generic error occurred in GDI+
谷歌搜索这似乎是当您无法保存文件时的一般错误,因为其他进程正在使用该位置。我假设多个上传试图同时保存在同一个文件夹中并且一个死了。它们不是同一个文件名。
我怎样才能避免这种情况?有没有办法让线程在尝试保存在同一个文件夹之前等待一个完成?我想知道是否有一种类似于 await 调用的方法,它会一直等待直到可以保存。
编辑更多代码
我无法复制整个函数,因为它很长等,但保存的部分如下:
//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 添加了一些代码。
-
尝试将
lock用于具有文件I/O 操作的函数/代码 -
@YvetteColomb 我想可能会有中断。就我如何处理连接和线程而言....不确定。 Dropzone 自己处理将所有文件发送给我,我在问题中没有显示的所有代码只是标准 c# 的业务逻辑代码。我没有对线程或类似的事情做任何具体的事情。
标签: c# asp.net-mvc image-uploading dropzone.js