ファイルのアップロード

ichiro2622004-10-20


ASP.NETでファイルのアップロードに成功しました。
PostingAcceptorやWebDAVと比べて、格段に使いやすくなっています。

aspxファイル

<html>
<body>
  <form id="Form1" method="post" runat="server" enctype="multipart/form-data">
    <asp:Label id="Label1" runat="server">ステータス</asp:Label></br>
    添付ファイルの選択:<INPUT type="file" id="File1" name="File1" runat="server"></br>
    保存時のファイル名:<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></br>
    <asp:Button id="Button1" runat="server" Text="ファイルの追加"></asp:Button></br>
  </form>
</body>
</html>

aspx.vbファイル

Private Sub Button1_Click(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles Button1.Click
  Dim LstrTempFolderName As String = "C:?Inetpub?wwwroot?Upload?"
  If TextBox1.Text = "" Then
    Label1.Text = "ファイル名を入力してください"
  Else
    If Not (File1.PostedFile Is Nothing) Then
      Try
        File1.PostedFile.SaveAs((LstrTempFolderName & TextBox1.Text))
        Label1.Text = "アップロード成功!<b>" & LstrTempFolderName _
              & TextBox1.Text & "</b>"
      Catch exc As Exception
        Label1.Text = "アップロード失敗!<b>" & LstrTempFolderName _
              & TextBox1.Text & "</b><br>" & exc.ToString()
      End Try
      TextBox1.Text = ""
    End If
  End If
End Sub