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: Code: 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.FileName.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.
You can control the permissions of your directories in you control panel on Permission Manager page. Please review this article on how to use Permission Manager.
Thanks for your help. I checked out the Permissions Manager link you posted, but my Permission Manager on my account does not have "anonymous user" as an option. Got any ideas how I could make that happen? Thanks.
Well, I adjusted the path and now there isn't any error. It even gives me the confirmation message that it did upload. However, when I check for the uploaded file, it is not there. Any ideas? Thanks.
The "filPhoto" is called out in the page that is accessed prior to the code I outlined above. Thanks for your help. Code: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Photo Upload</title> <style type="text/css"> body,input,form,span,div{font-family:Tahoma;font-size:8pt;} </style> <script type="text/javascript"> /* <![CDATA[ */ var PROGRESS_INTERVAL = 500; var PROGRESS_COLOR = '#000080'; var _divFrame; var _divUploadMessage; var _divUploadProgress; var _ifrPhoto; var _loopCounter = 1; var _maxLoop = 10; var _photoUploadProgressTimer; function initPhotoUpload() { _divFrame = document.getElementById('divFrame'); _divUploadMessage = document.getElementById('divUploadMessage'); _divUploadProgress = document.getElementById('divUploadProgress'); _ifrPhoto = document.getElementById('ifrPhoto'); var btnUpload = _ifrPhoto.contentWindow.document.getElementById('btnUpload'); btnUpload.onclick = function(event) { var filPhoto = _ifrPhoto.contentWindow.document.getElementById('filPhoto'); //Baisic validation for Photo _divUploadMessage.style.display = 'none'; if (filPhoto.value.length == 0) { _divUploadMessage.innerHTML = '<span style=\"color:#ff0000\">Please specify the file.</span>'; _divUploadMessage.style.display = ''; filPhoto.focus(); return; } var regExp = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF|.png|.PNG|.bmp|.BMP)$/; if (!regExp.test(filPhoto.value)) //Somehow the expression does not work in Opera { _divUploadMessage.innerHTML = '<span style=\"color:#ff0000\">Invalid file type. Only supports jpg, gif, png and bmp.</span>'; _divUploadMessage.style.display = ''; filPhoto.focus(); return; } beginPhotoUploadProgress(); _ifrPhoto.contentWindow.document.getElementById('photoUpload').submit(); _divFrame.style.display = 'none'; } } function beginPhotoUploadProgress() { _divUploadProgress.style.display = ''; clearPhotoUploadProgress(); _photoUploadProgressTimer = setTimeout(updatePhotoUploadProgress, PROGRESS_INTERVAL); } function clearPhotoUploadProgress() { for (var i = 1; i <= _maxLoop; i++) { document.getElementById('tdProgress' + i).style.backgroundColor = 'transparent'; } document.getElementById('tdProgress1').style.backgroundColor = PROGRESS_COLOR; _loopCounter = 1; } function updatePhotoUploadProgress() { _loopCounter += 1; if (_loopCounter <= _maxLoop) { document.getElementById('tdProgress' + _loopCounter).style.backgroundColor = PROGRESS_COLOR; } else { clearPhotoUploadProgress(); } if (_photoUploadProgressTimer) { clearTimeout(_photoUploadProgressTimer); } _photoUploadProgressTimer = setTimeout(updatePhotoUploadProgress, PROGRESS_INTERVAL); } function photoUploadComplete(message, isError) { clearPhotoUploadProgress(); if (_photoUploadProgressTimer) { clearTimeout(_photoUploadProgressTimer); } _divUploadProgress.style.display = 'none'; _divUploadMessage.style.display = 'none'; _divFrame.style.display = ''; if (message.length) { var color = (isError) ? '#ff0000' : '#008000'; _divUploadMessage.innerHTML = '<span style=\"color:' + color + '\;font-weight:bold">' + message + '</span>'; _divUploadMessage.style.display = ''; if (isError) { _ifrPhoto.contentWindow.document.getElementById('filPhoto').focus(); } } } /* ]]> */ </script> </head> <body> <form id="form1" runat="server"> <div style="width:400px"> <fieldset> <legend>Photo Upload</legend> <div id="divFrame"> <iframe id="ifrPhoto" onload="initPhotoUpload()" scrolling="no" frameborder="0" hidefocus="true" style="text-align:center;vertical-align:middle;border-style:none;margin:0px;width:100%;height:55px" src="FileUpload.aspx"></iframe> </div> <div id="divUploadMessage" style="padding-top:4px;display:none"></div> <div id="divUploadProgress" style="padding-top:4px;display:none"> <span style="font-size:smaller">Uploading photo...</span> <div> <table border="0" cellpadding="0" cellspacing="2" style="width:100%"> <tbody> <tr> <td id="tdProgress1"> </td> <td id="tdProgress2"> </td> <td id="tdProgress3"> </td> <td id="tdProgress4"> </td> <td id="tdProgress5"> </td> <td id="tdProgress6"> </td> <td id="tdProgress7"> </td> <td id="tdProgress8"> </td> <td id="tdProgress9"> </td> <td id="tdProgress10"> </td> </tr> </tbody> </table> </div> </div> </fieldset> </div> </form> </body> </html>