|
Dear all,
I should probually know better, but after 7/8 years of not touching ASP I have come back to it to make a personal website I have had an idea for.
I was never that great in the first place, but everything has moved on and I a struggling. I have managed to contenect to my MS SQL database and manipulate data in and out using stored Procs (which I was pleased with), but I have come up against a total brick wall with regard to uploading images.
I need to allow users to upload a jpeg image, then automatically resize it and save two copies one large (max 500px wide) and one small (100px) in a directory on my site (uploaded_images).
I am using GoDaddy and I have created a folder with read & write permissions.
I know that to do this in traditional ASP I would need to use AspUpload, but I cannot afford the £££'s that would cost.
Everywhere I look tells me that I can do it in 'one or two easy lines of code' in Asp.Net, but I have never used this and so quite out of my depth.
I have downloaded this code which I run as upload.aspx, but I just get a runtime error (which is the first time i have seen this error message - I am used to the old ASP errors)
The code is:
<%@ Page Language="vb" %>
<html>
<Script Language="VB" RunAt="Server">
Sub Page_Load(Sender as Object, e as EventArgs)
Dim MyPath, MyName as string
' Display the names in C:\ that represent directories.
MyPath = "/uploaded_images/" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
if MyName="" ' The folder is not there & to be created
MkDir("/uploaded_images/") 'Folder created
span2.InnerHtml="A Folder (/uploaded_images/) is created at the Page_Load"
end if
End Sub
Sub Upload_Click(Sender as Object, e as EventArgs)
' Display properties of the uploaded file
FileName.InnerHtml = MyFile.PostedFile.FileName
FileContent.InnerHtml = MyFile.PostedFile.ContentType
FileSize.InnerHtml = MyFile.PostedFile.ContentLength
UploadDetails.visible = True
' Let us recover only the file name from its fully qualified path at client
Dim strFileName as string
strFileName = MyFile.PostedFile.FileName
Dim c as string = System.IO.Path.GetFileName(strFileName) ' only the attched file name not its path
' Let us Save uploaded file to server at /uploaded_images/
Try
MyFile.PostedFile.SaveAs("/uploaded_images/" + c)
Span1.InnerHtml = "Your File Uploaded Sucessfully at server as : /uploaded_images/" & c
catch Exp as exception
span1.InnerHtml = "An Error occured. Please check the attached file"
UploadDetails.visible = false
span2.visible=false
End Try
End Sub
</Script>
<Body>
<Font Color="DarkGreen" Face=Helvetica Size=5> <B>Uploading In ASP.Net - A Rich Uploading !
</Font>
<HR Size="2" Color=Black >
<P>
<Form Method="Post" EncType="Multipart/Form-Data" RunAt="Server">
Choose Your File To Upload : <BR>
<Input ID="MyFile" Type="File" RunAt="Server" Size="40"> <BR>
<BR>
<Input Type="Submit" Value="Upload" OnServerclick="Upload_Click" RunAt="Server">
<P>
<Div ID="UploadDetails" Visible="False" RunAt="Server">
File Name: <Span ID="FileName" RunAt="Server"/> <BR>
File Content: <Span ID="FileContent" RunAt="Server"/>
<BR>
File Size: <Span ID="FileSize" RunAt="Server"/>bytes
<BR></Div>
<Span ID="Span1" Style="Color:Red" RunAt="Server"/>
<Span ID="Span2" Style="Color:Red" RunAt="Server"/>
</Form>
<HR Size="2" Color=Black>
</Body>
</html>
I am not expecting people to write the code for me (although I would never look a gift horse in the mouth!), but if you could point me in the direct of a really simple step-by-step tutorial that would be great or make some suggestions on the above.
Many thanks,
James
|