Skip to content

Instantly share code, notes, and snippets.

@peerasak-u
Created September 6, 2013 07:09
Show Gist options
  • Select an option

  • Save peerasak-u/6460481 to your computer and use it in GitHub Desktop.

Select an option

Save peerasak-u/6460481 to your computer and use it in GitHub Desktop.
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Data
Imports Newtonsoft.Json
Namespace UploadFile
Public Class UploadForm
Inherits System.Web.UI.Page
Protected Overrides Sub OnInit(e As EventArgs)
InitializeComponent()
MyBase.OnInit(e)
End Sub
Private Sub InitializeComponent()
Me.Load += New System.EventHandler(AddressOf Me.Page_Load)
End Sub
Private Sub Page_Load(sender As Object, e As System.EventArgs)
Dim files As HttpFileCollection = Request.Files
If files.Count = 0 Then
Response.Write("no file")
ElseIf files.Count <> 1 Then
Response.Write("too much files")
Else
Response.Write("file OK")
' Get a reference to PostedFile object
Dim file As HttpPostedFile = files(0)
' Get size of uploaded file
Dim nFileLen As Integer = file.ContentLength
' make sure the size of the file is > 0
If nFileLen > 0 Then
' Allocate a buffer for reading of the file
Dim myData As Byte() = New Byte(nFileLen - 1) {}
' Read uploaded file from the Stream
file.InputStream.Read(myData, 0, nFileLen)
' Create a name for the file to store
Dim strFilename As String = Path.GetFileName(file.FileName)
' Write data into a file
WriteToFile(Server.MapPath(strFilename), myData)
End If
End If
End Sub
Private Sub WriteToFile(strPath As String, ByRef Buffer As Byte())
' Create a file
Dim newFile As New FileStream(strPath, FileMode.Create)
' Write data to the file
newFile.Write(Buffer, 0, Buffer.Length)
' Close file
newFile.Close()
End Sub
End Class
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment