- PR -

FileUpload で重複の確認

1
投稿者投稿内容
SL
大ベテラン
会議室デビュー日: 2008/05/02
投稿数: 183
投稿日時: 2008-12-13 22:09
お世話になります。

FileUploadでファイルをアップするとき、重複するファイルを見つけたとき、
上書きするかどうか確認するダイアログを表示させたいのですが、
下記の処理では、サーバー側のためダイアログは表示しますがプログラムは、スルーします。
当然?なのかもしれませんが、これをダイアログクリックで処理したいのですが、
どう処理すればいいでしょうか?

おそらく、JavaScriptあたりで有無を表示?と思うのですがサーバー側から呼べないと思います。それでどうするのか考えあぐねています。
教えてください。よろしくお願いします。

コード:
string UpFolder_URL = "~/Data/";
string UpFolder = Server.MapPath(UpFolder_URL);
string Label_Error = "";

if (FileUpload1.HasFile)
{
     // 画像ファイルをアップロード用フォルダに保存する
     string ImageFile = UpFolder + FileUpload1.FileName;
     if (File.Exists(ImageFile))
      {
           // 既に同名のファイルが有る場合には保存せずに終了する
           Label_Error = "同じファイル名が既に存在します。上書きしますか?";

           string stResavationData = string.Format("confirm('{0}');", Label_Error);
           string startupScript = string.Format("<script type='text/javascript'> {0} <" + "/script>", stResavationData);
           Page.ClientScript.RegisterStartupScript(this.GetType(), "startup", startupScript);

           FileUpload1.SaveAs(ImageFile);  // 上書き確認をしたいのですが、Yes,noを推す前にここをスルーしてしまう。
           return;
       }
       FileUpload1.SaveAs(ImageFile);
            :


SL
大ベテラン
会議室デビュー日: 2008/05/02
投稿数: 183
投稿日時: 2008-12-13 23:07
お世話になります。

自己レスです。

Page_Load() に下記を追加することで回避できました。
Button2.Attributes["onclick"] = "return confirm('同じ名前のファイルが存在します。上書きしますか?');";
King
ぬし
会議室デビュー日: 2008/06/20
投稿数: 284
投稿日時: 2008-12-14 02:31
どういう条件でそのロジックを実行するよう追加したのかわからないので
はっきりとは言えませんが
そのやり方だと重複するファイルがあろうが無かろうが
上書き確認のダイアログが出ませんか?
Access
ぬし
会議室デビュー日: 2002/04/08
投稿数: 829
投稿日時: 2008-12-14 06:27
重複するときはファイル名を書き換えて保存するのが一般的なようですね。

コード:
    Sub SaveFile(ByVal file As HttpPostedFile)

        ' Specify the path to save the uploaded file to.
        Dim savePath As String = "c:\\\\\\\\temp\\\\\\\\uploads\\\\\\\\"

        ' Get the name of the file to upload.
        Dim fileName As String = FileUpload1.FileName

        ' Create the path and file name to check for duplicates.
        Dim pathToCheck As String = savePath + fileName

        ' Create a temporary file name to use for checking duplicates.
        Dim tempfileName As String

        ' Check to see if a file already exists with the
        ' same name as the file to upload.        
        If (System.IO.File.Exists(pathToCheck)) Then
          Dim counter As Integer = 2
          While (System.IO.File.Exists(pathToCheck))
            ' If a file with this name already exists,
            ' prefix the filename with a number.
            tempfileName = counter.ToString() + fileName
            pathToCheck = savePath + tempfileName
            counter = counter + 1
          End While

          fileName = tempfileName

          ' Notify the user that the file name was changed.
          UploadStatusLabel.Text = "A file with the same name already exists." + "<br />" + _
                                   "Your file was saved as " + fileName

        Else

          ' Notify the user that the file was saved successfully.
          UploadStatusLabel.Text = "Your file was uploaded successfully."

        End If

        ' Append the name of the file to upload to the path.
        savePath += fileName

        ' Call the SaveAs method to save the uploaded
        ' file to the specified directory.
        FileUpload1.SaveAs(savePath)

      End Sub




http://msdn.microsoft.com/ja-jp/library/system.web.ui.webcontrols.fileupload.saveas.aspx

_________________
ASP.NET+Ajaxサンプル集 | JavaScript+Ajaxサンプル集
SL
大ベテラン
会議室デビュー日: 2008/05/02
投稿数: 183
投稿日時: 2008-12-14 10:26
お世話になります。

> 重複するファイルがあろうが無かろうが .......
そうでした。このやり方では、毎回出ます。

セキュリティ上も含め上書きができるようになってるとまずそうですね。
提示していただいたように「ファイル名を変更する」で対応していきたいと思います。

ありがとうございました。
なちゃ
ぬし
会議室デビュー日: 2003/06/11
投稿数: 872
投稿日時: 2008-12-14 14:51
そもそもアップロードで指定されたファイル名で保存すること自体をおすすめしません。
まあきちんと安全に作れるならいいですが。
1

スキルアップ/キャリアアップ(JOB@IT)