Click to See Complete Forum and Search --> : Uploading files and displaying pics from a database(Access)


Calmaris
11-21-2003, 08:00 AM
Hey I'm back, I've been gone for awhile but I'm back to designing pages again.
PROBLEM: I can't figure out how to have users upload a picture to my server, from a web page. Also I'm racking my brain at trying to figure out how to have the asp page make a directory for each new registered user to store pictures in and have the folder automatically named after the user's Id. Also the best way to display the pictures on the page, I'm using Access database and when I just use the ole to link the picture only the address of the picture shows up on the web page.

I'm sorry I can't supply any of my meager code for this, because my work won't allow it. :(. If someone could explain this too me with some examples that would great...and save me from having an anuerism! Thx
Alex

cmelnick
11-24-2003, 01:39 PM
Check out my upload tutorial (http://www.aardwulf.com/tutor/uploader) for an upload script you can download and use. It is pretty easy to use, just let me know if you need more help.

Creating a directory for a new member is very easy, you just use the VBScript FileSystem object:


Dim fileSys, newFolderStr, newFolder
fileSys = Server.CreateObject("Scripting.FileSystemObject")

newFolderStr = "C:\yourWebDir\members\" & newUsername

If Not fileSys.FolderExists(newFolderStr) Then

newFolder = fileSys.CreateFolder(newFolderStr)

End If


Check out the gurus (http://www.devguru.com/Technologies/vbscript/quickref/filesystemobject.html) for complete info on the FileSystemObject.

As far as displaying the picture goes, if you are returning just the address of the picture, just use the <img src="<%=addressOfThePicture%>"> to display it on a web page.

CardboardHammer
11-24-2003, 07:29 PM
If you can use ASP.NET instead of ASP for the relevant pages, it's somewhat easier and quicker as well... I've got file upload code somewhere done in VB.NET and I can look it up when I wander (<- edit for spelling) back to work later...

PeOfEo
11-24-2003, 07:58 PM
Would you quit pimping asp.net when the person is using asp classic. asp.net is nice but these people want useful asp help and not a sales pitch for .net

CardboardHammer
11-24-2003, 09:26 PM
Eh? I'm offering code that will handle file uploads. The thing is, it's ASP.NET. If he wants it he can have it, but if it can't be useful to him (i. e. he isn't allowed to use ASP.NET), then no big deal...

And you can run ASP.NET and ASP side by side within the same web without a problem, so it's not like he'd have to redo everything to use my code...

Calmaris
11-25-2003, 06:31 AM
Thx alot for your help guys. Well 3 days ago I decided get away from classic asp and go asp.net (Mostly for the debugging because it's way more specific) So I did the transition of my site from asp to asp.net(minus much lost hair and broken coaster) so I definetly could use the code in asp.net, Thx in advance for the help very much appreciated!
Alex

CardboardHammer
11-25-2003, 09:22 AM
OK. I'll dig it up when I get back to work... should be posted before tomorrow AM. IIRC, it allows for generic file uploading and size limitation of file uploads at the current moment... I can help you tweak it to fit more specific needs.

CardboardHammer
11-25-2003, 01:18 PM
Here it is (yeah, it's not the greatest, but it does work... it was in my early days with ASP.NET (this code is over a year old) and was just intended as a learning experience, these days I put all my server side code in the code behind files to keep things cleaner looking (BTW, this isn't in production either, or I would have cleaned it up quite a bit)). You'll need to change strFilePath to something appropriate for your server. Note that as it is, the password is sent in the clear, so SSL would be needed to keep the password secure.

I'll be recoding this eventually as a future project here at work will be calling for a web based "file cabinet" for various staff members. Each person will get their own "cabinet" and will be able to add/view/edit/remove items they control to their cabinet. Certain higher level users will be able to view all contents of user "cabinets" and/or deposit certain files into the user's "cabinet", which will not necessarily be editable/deletable/viewable by the "cabinet" owner.

fileupload.aspx
<%@ Page Language="VB"
Explicit="True" %>
<HTML>
<script runat="server">
Sub UploadBtn_Click(Sender as Object, e as EventArgs)
Dim strFileName As String
Dim strFilePath As String
Dim strPass As String
Dim lngLength As Long

lngLength = MyFile.PostedFile.ContentLength

strPass = "0987654321qazxcvbnm,./']=-"

If StrComp(txtPwd.Text, strPass, 0) <> 0 Then
FileName.InnerHtml = "File not saved, invalid password!"
Else
strFilePath = "DIRECT_PATH_TO_THE_WEB_GOES_HERE\THIS_IS_THE_SUBDIRECTORY_FOR_THE_FILES_TO_BE_UPLOADED_TO\"
strFileName = MyFile.PostedFile.FileName
strFileName = Right(strFileName, strFileName.length() - InStrRev(strFileName, "\"))


If lngLength > 1024000000 Then
FileName.InnerHtml = "File not saved, too big."
Else
' Display information about posted file
'FileName.InnerHtml = MyFile.PostedFile.FileName
FileName.InnerHtml = strFileName

' Save uploaded file to server
MyFile.PostedFile.SaveAs(strFilePath & strFileName)

End If
End If
MyContentType.InnerHtml = MyFile.PostedFile.ContentType
ContentLength.InnerHtml = cStr(lngLength)
FileDetails.Visible = True
End Sub
</script>
<body>
<form id="Form1" action="fileupload.aspx" method="post" encType="multipart/form-data" runat="server">
<h1>ASP.NET File Upload Example</h1>
<P>Select File To Upload to Server: <input id="MyFile" type="file" name="MyFile" runat="server">
<br>
</P>
<asp:Label id="lblPwd" runat="server">Password: </asp:Label>
<asp:TextBox id="txtPwd" runat="server" MaxLength="50" TextMode="Password"></asp:TextBox>
<br>
<br>
<input type="submit" value="Upload!" OnServerclick="UploadBtn_Click" runat="server" ID="Submit1" NAME="Submit1">
<br>
<br>
<br>
<div id="FileDetails" Visible="false" runat="server">
FileName:
<span id="FileName" runat="server"></span><br>
ContentType:
<span id="MyContentType" runat="server"></span><br>
ContentLength:
<span id="ContentLength" runat="server">bytes</span>
<br>
</div>
</form>
</body>
</HTML>


fileupload.aspx.vb
Public Class fileupload
Inherits System.Web.UI.Page
Protected WithEvents MyFile As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Submit1 As System.Web.UI.HtmlControls.HtmlInputButton
Protected WithEvents FileName As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents MyContentType As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents txtPwd As System.Web.UI.WebControls.TextBox
Protected WithEvents lblPwd As System.Web.UI.WebControls.Label
Protected WithEvents FileDetails As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents ContentLength As System.Web.UI.HtmlControls.HtmlGenericControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

End Class