2017-08-21 23 views
-1

私はdropzone.jsを使用して複数の画像を.NET MVC Webサイトにアップロードしています。イメージがサーバーに入ると、イメージのサイズを変更し、データベースに情報を保存し、実際のイメージをサーバーのフォルダに保存します。私は彼らにこの作品を一度にアップロードが、私は一度に複数のアップロードを行う場合、私はすべてのいくつかのアップロードこのエラーを取得する場合:ASP.NET MVC Webサイトで複数の画像アップロードを処理するには?

A generic error occurred in GDI+ 

はこれをグーグルであなたが保存することができないとき、これは一般的なエラーであると思われます他のプロセスがその場所を使用しているので、ファイル。私は、複数のアップロードが同じフォルダに同時に保存しようとしていると仮定し、1つは死ぬ。それらは同じファイル名ではありません。

どうすればこの問題を回避できますか?同じフォルダに保存しようとする前にスレッドが完了するのを待つ方法がありますか?私はそれが保存するまで待機する待っている呼び出しのようなメソッドがあるかどうか疑問に思っています。

編集より多くのコード

、それは非常に長いなどだが、節約の部分は以下の通りですように私は私の全体の機能をコピーすることはできません。

//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}"; 
} 
+0

が同じフォルダに保存されているこれらの画像が同じファイル名を持っていますか役に立てば幸い?このデータをどのように保存するかに関して、あなたの質問に付随するいくつかのコードを提供する必要があります。 –

+0

@DanielShillcockいくつかのコードを追加しました。 – SolidSnake4444

+0

ファイルI/O操作で関数/コードに 'lock'を使ってみてください –

答えて

0

通常、このエラーの一般的なエラーoccurred in GDI+は、そのファイルを意味しますか

ディレクトリが存在するかどうかを確認したり、作成していない場合は作成してください。同じ名前のファイルが既にあるかどうか、またはフルパスが存在するかどうかを再度確認してください。

試し:

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); 

が、それはあなた

関連する問題