mattnkara
11-13-2009, 10:11 AM
Hi. I am having a problem getting a file upload control to work. I came to DiscountASP.net from GoDaddy because it was my understanding that they offered "full control" (I guess) so that members of my site can upload files? I am not sure how to change the folder permission to allow "full control".
Here is my current code-behind:
Imports System.IO
Imports System.Drawing
Imports System.Web.UI.HtmlControls
Partial Class FileUpload
Inherits System.Web.UI.Page
Private Const SCRIPT_TEMPLATE As String = "<" + "script " + "type=""text/javascript"">window.parent.photoUploadComplete('{0}', {1});" + "<" + "/script" + ">"
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Session("UserName") = HttpContext.Current.User.Identity.Name
If IsPostBack Then
Dim UpPath As String
UpPath = "~/Clients/Logos/"
If Not Directory.Exists(UpPath) Then
Directory.CreateDirectory("~/Clients/Logos/")
End If
'Sleeping for 10 seconds, fake delay, You should not it try at home.
System.Threading.Thread.Sleep(3000)
UploadPhoto()
End If
End Sub
Private Sub UploadPhoto()
Dim script As String = String.Empty
If (filPhoto.PostedFile IsNot Nothing) AndAlso (filPhoto.PostedFile.ContentLength > 0) Then
If Not IsValidImageFile(filPhoto) Then
script = String.Format(SCRIPT_TEMPLATE, "The uploaded file is not a valid image file.", "true")
End If
Else
script = String.Format(SCRIPT_TEMPLATE, "Please specify a valid file.", "true")
End If
If filPhoto.PostedFile.ContentLength > 150 * 1024 Then
'resrticting to 150 KB
script = String.Format(SCRIPT_TEMPLATE, "The file is too large. Please limit size to 150 kb.", "true")
End If
If String.IsNullOrEmpty(script) Then
'Uploaded file is valid, now we can do whatever we like to do, copying it file system,
'saving it in db etc.
Dim strFileName As String
strFileName = filPhoto.PostedFile.FileName
Dim fileExt As String = System.IO.Path.GetExtension(filPhoto.PostedFile.Fi leName.ToLower())
Dim c As String = Session("UserName")
'Dim c As String = System.IO.Path.GetFileName(strFileName)
Try
filPhoto.PostedFile.SaveAs(Server.MapPath("~/Clients/Logos/" + c + fileExt))
script = String.Format(SCRIPT_TEMPLATE, "File was uploaded. Please click Finish below.", "false")
Catch exp As Exception
script = String.Format(SCRIPT_TEMPLATE, "File upload FAILED. Please try again.", "false")
End Try
End If
'Now inject the script which will fire when the page is refreshed.
ClientScript.RegisterStartupScript(Me.[GetType](), "uploadNotify", script)
End Sub
Private Shared Function IsValidImageFile(ByVal file As HtmlInputFile) As Boolean
Try
Using bmp As New Bitmap(file.PostedFile.InputStream)
Return True
End Using
'throws exception if not valid image
Catch generatedExceptionName As ArgumentException
End Try
Return False
End Function
End Class
Also, I am wanting to limit file sizes (as it refers to picture width and height). You can see here that i have limited the size of the picture, does anyone know how to limit width and height as well?
Thanks.
Here is my current code-behind:
Imports System.IO
Imports System.Drawing
Imports System.Web.UI.HtmlControls
Partial Class FileUpload
Inherits System.Web.UI.Page
Private Const SCRIPT_TEMPLATE As String = "<" + "script " + "type=""text/javascript"">window.parent.photoUploadComplete('{0}', {1});" + "<" + "/script" + ">"
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Session("UserName") = HttpContext.Current.User.Identity.Name
If IsPostBack Then
Dim UpPath As String
UpPath = "~/Clients/Logos/"
If Not Directory.Exists(UpPath) Then
Directory.CreateDirectory("~/Clients/Logos/")
End If
'Sleeping for 10 seconds, fake delay, You should not it try at home.
System.Threading.Thread.Sleep(3000)
UploadPhoto()
End If
End Sub
Private Sub UploadPhoto()
Dim script As String = String.Empty
If (filPhoto.PostedFile IsNot Nothing) AndAlso (filPhoto.PostedFile.ContentLength > 0) Then
If Not IsValidImageFile(filPhoto) Then
script = String.Format(SCRIPT_TEMPLATE, "The uploaded file is not a valid image file.", "true")
End If
Else
script = String.Format(SCRIPT_TEMPLATE, "Please specify a valid file.", "true")
End If
If filPhoto.PostedFile.ContentLength > 150 * 1024 Then
'resrticting to 150 KB
script = String.Format(SCRIPT_TEMPLATE, "The file is too large. Please limit size to 150 kb.", "true")
End If
If String.IsNullOrEmpty(script) Then
'Uploaded file is valid, now we can do whatever we like to do, copying it file system,
'saving it in db etc.
Dim strFileName As String
strFileName = filPhoto.PostedFile.FileName
Dim fileExt As String = System.IO.Path.GetExtension(filPhoto.PostedFile.Fi leName.ToLower())
Dim c As String = Session("UserName")
'Dim c As String = System.IO.Path.GetFileName(strFileName)
Try
filPhoto.PostedFile.SaveAs(Server.MapPath("~/Clients/Logos/" + c + fileExt))
script = String.Format(SCRIPT_TEMPLATE, "File was uploaded. Please click Finish below.", "false")
Catch exp As Exception
script = String.Format(SCRIPT_TEMPLATE, "File upload FAILED. Please try again.", "false")
End Try
End If
'Now inject the script which will fire when the page is refreshed.
ClientScript.RegisterStartupScript(Me.[GetType](), "uploadNotify", script)
End Sub
Private Shared Function IsValidImageFile(ByVal file As HtmlInputFile) As Boolean
Try
Using bmp As New Bitmap(file.PostedFile.InputStream)
Return True
End Using
'throws exception if not valid image
Catch generatedExceptionName As ArgumentException
End Try
Return False
End Function
End Class
Also, I am wanting to limit file sizes (as it refers to picture width and height). You can see here that i have limited the size of the picture, does anyone know how to limit width and height as well?
Thanks.