Upload file di ASP.NET dapat menggunakan ASP.NET Web Control FileUpload. Untuk implementasi upload tinggal pasang control FileUpload dan satu Button.
<asp:FileUpload ID="fileUpload" runat="server" Width="300" />
<asp:Button id="btnUpload" runat="server" Text="Upload" />
Pada codebehind, buat handler untuk btnUpload.Click
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
Try
Dim objFile As FileInfo = New FileInfo
If fileUpload.PostedFile Is Nothing Then
Exit Sub
End If
If Not fileUpload.HasFile Then
Exit Sub
End If
Dim path As String = Me.MapPath("files/")
path = path.Replace("/", "\")
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
fileUpload.SaveAs(path + fileUpload.PostedFile.FileName)
Catch exc As Exception
'Exception code here
End Try
End Sub
Untuk download file, kita bisa menggunakan Response untuk mengirimkan data ke client. Misalkan ada sebuat tombol untuk download, handler tombol tersebut dapat ditulis sebagai berikut.
Dim path As String = Me.MapPath("files/")
path = path.Replace("/", "\")
Dim namafile As String = "namafile.ext"
Response.Clear() 'Bersihkan buffer stream
'Tambahkan header namafile
Response.AppendHeader("content-disposition", "attachment; filename=" + namafile)
Response.WriteFile(path + namafile)
Response.End()
Response.Close()
Jika file yang diupload perlu diolah terlebih dahulu atau disimpan dalam database, maka file tersebut dapat dibaca menggunakan BinaryReader. Berikut contoh sederhana untuk melakukan enkripsi sebelum file disimpan di filesystem atau database.
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
Try
Dim objFile As FileInfo = New FileInfo
If fileUpload.PostedFile Is Nothing Then
Exit Sub
End If
If Not fileUpload.HasFile Then
Exit Sub
End If
Dim path As String = Me.MapPath("files/")
path = path.Replace("/", "\")
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
Dim uploadedStream As New BinaryReader(fileUpload.PostedFile.InputStream)
Dim bytesStream As Byte() = uploadedStream.ReadBytes(fileUpload.PostedFile.ContentLength)
'Tutup stream
uploadedStream.Close()
'Panggil fungsi enkripsi (contoh)
Dim cipherStream As Byte() = Encrypt(bytesStream)
'Simpan hasil enkripsi dalam filesystem
Using _fileStream As New FileStream(path + fileUpload.FileName, FileMode.Create, FileAccess.Write)
_fileStream.Write(cipherStream, 0, cipherStream.Length)
_fileStream.Flush()
_fileStream.Close()
End Using
Catch exc As Exception
'Exception code here
End Try
End Sub
Jika file telah dienkripsi, maka pada waktu download file tersebut juga harus didekripsi sebelum dikirimkan ke client.
path = path.Replace("/", "\")
Dim namafile As String = "namafile.ext"
'Buka stream
Dim _fileStream As New FileStream(path + namafile, FileMode.Open, FileAccess.Read)
Dim _binReader As New BinaryReader(_fileStream)
Dim _cipherStream As Byte() = _binReader.ReadBytes(_fileStream.Length)
_binReader.Close()
_fileStream.Close()
'Panggil fungsi dekripsi (contoh)
Dim _plainStream As Byte() = Decrypt(_cipherStream)
'Kirim stream ke client
Response.Clear() 'Bersihkan buffer stream
'Tambahkan header namafile
Response.AppendHeader("content-disposition", "attachment; filename=" + namafile)
Response.BinaryWrite(plainStream)
Response.Flush()
Response.End()
Response.Close()
Semoga bermanfaat