Is This Possible ... ?

W

Wayne Smith

I've come up against a major headache that I can't seem to find a solution
for but I'm sure there must be a workaround and I would really be grateful
of any help.

I'm currently building a web site for a small club I belong to and one of
the features I would like to include is the ability to allow users to upload
image files.

unfortunately the servers web root www folder only allows READ and EXECUTE
permissions, which makes it impossible to allow a user to upload a gif/jpeg
image. When I FTP into the site however, I can immediately see two folders:

databases
www

the www folder is the root folder and all web pages need to be placed there,
but the databases folder does have Read, WRITE and Execute permissions and I
have been reliably informed that some ASP.NET code would allow a user to
send a picture file to an Access database in this folder, the problem I have
is not knowing how to reference it because it's not directly accessible from
the Internet - i.e. www.website.co.uk/databases would not work.

The server is running ASP.NET 1.1 and the permissions setup has to stay the
way it is, I'm not able to get my host to change the permissions on the root
folder so I have to try and make use of the databases folder instead. For
arguments sake lets also assume I already have a simple Access database
called images.mdb with one table called Images, which in turn contains two
fields, one called ID (Autonumber) (Primary Key) and another called images
(OLE Object)

What I would really like, if possible, is a very simple sample page with a
text box and a Browse button, with a second button to 'Upload' the file once
selected, plus any supporting pages that may be required to complete the
operation, i.e.:

selectfile.aspx (simple page to select image file and send to database)
uploadfile.aspx (works behind the scenes to transfer the file)
confirmation.aspx (a confirmation page to indicate the transfer either
succeeded or failed)

I'm not a natural programmer by any stretch of the imagination, which is why
I'm being a bit cheeky and asking for a very simple layout to accomplish
this task, or a straight forward dummies tutorial which takes into
consideration the problem with uploading to a database outside of the root
web.

If anybody can help me solve this problem, I will forever be indebted to
you. I have searched the Internet for over a week and remarkably I cannot
find an answer to this problem, it's now driving me nuts - I just want to
solve this so I can move on with the rest of the site design.

Any help would be greatly appreciated.

Many thanks in advance
Wayne
 
C

clintonG

The news article started earlier in the day of 6/23 "FTP from one website to
another?" has just started discussing the same thing.

For the form and uploading interface you can pick up tuts and copy and paste
source anywhere. Its how to reference the target file system and use the FTP
protocol to PUT the file where it is wanted. There are many control
developers who have developed controls that support FTP. What are you
planning to do next?

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
 
W

Wayne Smith

Clinton,
Thanks for your reply post.

To be honest I'm not really looking at the FTP protocol simply because as
the web site administrator, I have access to FTP into the site using my
login credentials, and to be honest I wouldn't want every user of the site
knowing those details just for the sake of uploading picture files - plus
there is always the risk that someone will just FTP into the site and add
and delete files at will.

On that basis I really need to stick with either classic ASP or ASP.NET 1.1,
which are both running on my hosting providers server.

I'm absolutely desperate for a solution to this problem so if you can offer
anything else in line with what I have mentioned here and in my original
post, or indeed if anyone else reading this may have a solution - PLEASE
HELP !!!

I'm starting to go grey

Thanks
Wayne
 
K

Ken Cox [Microsoft MVP]

Hi Wayne,

Okay, here's the code that should get you going. You'll need an Access
database (called imagedb.mdb) in the App_Data folder with this schema:

id: AutoNumber
FileName:Text
FileSize: Number (long integer)
ContentType: Text
FileData: OLE Object

There are two ASP.NET files. Upload.aspx handles the upload and display.
Imagefetch.aspx is a helper page that provides the image to the datagrid.

This is rough code - barebones stuff with no error checking to speak of.
Most of the code is ripped off from other articles, especially from

File Uploading to Access Database using ASP.NET
by Faisal Khan.

http://www.stardeveloper.com/articles/display.html?article=2003031201&page=1

Anyway, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]

-- upload.aspx
<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnDisplay_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)

' Gets the image data and displays it
' in the datagrid
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
Dim con As New Data.OleDb.OleDbConnection(strConnection)
Dim cmdtext As String = "SELECT ID, FileName," _
& " FileSize, ContentType FROM Files"
Dim cmd As New Data.OleDb.OleDbCommand(cmdtext, con)
Dim ds As New Data.DataSet
Dim da As New Data.OleDb.OleDbDataAdapter _
(cmdtext, strConnection)
da.Fill(ds)
dg2.DataSource = ds
dg2.DataBind()
End Sub


Protected Sub btnUpload_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
' Puts uploaded file in Access database
' Adapted for VB by Ken Cox [MVP] from
' File Uploading to Access Database using ASP.NET
' by Faisal Khan.
' http://www.stardeveloper.com/articles/display.html
'?article=2003031201&page=1
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
If Request.Files.Count > 0 Then
Dim files As HttpFileCollection
Dim afile As HttpPostedFile
files = Request.Files
afile = files(0)
Dim contentType As String = afile.ContentType
Dim filename As String = ""
Dim filelength As Integer = afile.ContentLength
Dim fileData As Byte() = New Byte(filelength) {}
Dim lastpos As Integer
lastpos = afile.FileName.LastIndexOf("\")
filename = afile.FileName.Substring(lastpos + 1)
afile.InputStream.Read(fileData, 0, filelength)
Dim con As New Data.OleDb.OleDbConnection(strConnection)
Dim cmdtext As String = "INSERT INTO Files(FileName," & _
" FileSize, ContentType, FileData) VALUES " & _
" (@FileName, @FileSize, @ContentType, @FileData)"
Dim cmd As New Data.OleDb.OleDbCommand(cmdtext, con)
Dim pms As Data.OleDb.OleDbParameterCollection
pms = cmd.Parameters
pms.Add("@FileName", Data.OleDb.OleDbType.VarChar, 50)
pms.Add("@FileSize", Data.OleDb.OleDbType.Integer)
pms.Add("@ContentType", Data.OleDb.OleDbType.VarChar, 50)
pms.Add("@FileData", Data.OleDb.OleDbType.VarBinary)

pms("@FileName").Value = filename
pms("@FileSize").Value = filelength
pms("@ContentType").Value = contentType
pms("@FileData").Value = fileData

con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload and View Images in MS Access</title>
</head>
<body>
<form id="Form1" runat="server" enctype="multipart/form-data">
<input name="thefile" type="file"><br>
<br />
<asp:button id="btnUpload" runat="server" text="Upload"
onclick="btnUpload_Click" />&nbsp;<br />
<br />
<asp:button id="btnDisplay" runat="server"
onclick="btnDisplay_Click" text="View Images" />&nbsp;<br />
<br />
<asp:datagrid id="dg2" runat="server" autogeneratecolumns="False">
<columns>
<asp:boundcolumn datafield="filename" headertext="Filename"
readonly="True"></asp:boundcolumn>
<asp:boundcolumn datafield="Filesize" headertext="Filesize"
readonly="True"></asp:boundcolumn>
<asp:boundcolumn datafield="contenttype"
headertext="Contenttype" readonly="True"></asp:boundcolumn>
<asp:templatecolumn headertext="Image">
<itemtemplate>
<asp:image runat="server" imageurl='<%#
"imagefetch.aspx?id=" & DataBinder.Eval(Container, "DataItem.id") %>' />
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<br />
</form>
</body>
</html>

-- imagefetch.aspx

<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
' Gets the image from the Access
' Database for inclusion in another page
' Adapted from an MS KB article by Ken Cox [MVP]
'
Dim recno As Integer
If Request("id") = "" Then
Exit Sub
End If
recno = Request("id")
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
Dim con As New Data.OleDb.OleDbConnection _
(strConnection)
Dim da As New Data.OleDb.OleDbDataAdapter _
("Select filedata From files where id=" & _
recno.ToString, con)
Dim MyCB As New Data.OleDb.OleDbCommandBuilder(da)
Dim ds As New Data.DataSet()
con.Open()
da.Fill(ds, "images")
Dim myRow As Data.DataRow
myRow = ds.Tables("images").Rows(0)
Dim MyData() As Byte
MyData = myRow("filedata")
Response.Buffer = True
Response.ContentType = "Image/JPEG"
Response.BinaryWrite(MyData)
MyCB = Nothing
ds = Nothing
da = Nothing
con.Close()
con = Nothing
End Sub
</script>
 
K

Ken Cox [Microsoft MVP]

Hey Wayne,
I'm starting to go grey

What's wrong with going gray?

(I'm already gray - started in my twenties.)

Anyway, check my response to your first post?

Ken
Microsoft MVP [ASP.NET]
 
W

Wayne Smith

Hi Ken,
Many thanks for taking the time to post your response, it really is
appreciated.

I followed your instructions pretty much to the letter but I'm still coming
up with an error message, I'll try and be as detailed as I can with what I
have done so you may be able to spot something obvious that I can't see.

I created a database exactly as you said, the only change I made was to
upload it into the 'databases' folder, and not a folder called 'App_Data',
simply because when I FTP into my site the two folders I see are:

databases
www

And I didn't want to change anything with the 'databases' folder as that
currently has the Read, WRITE and Execute permissions, but to be fair that
shouldn't make much difference because I also changed the line of code in
three places which references 'App_Data/imagedb.mdb' to
'databases/imagedb.mdb'

That was the only change I made so I then uploaded the 'imagedb.mdb' file to
the 'databases' folder, and I uploaded both the 'upload.aspx' and
'imagefetch.aspx' files to the www/gallery folder - so far so good.

I then opened the site and navigated to the 'gallery' link from my home
page, the page displayed correctly giving me a text box with a browse
button, and underneath an 'upload' button and a 'view images' button.

I selected a gif file from my desktop and clicked 'upload', after a few
seconds the following error page was displayed:

--------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException:
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.

Source Error:


Line 68: pms("@FileData").Value = fileData
Line 69:
Line 70: con.Open()
Line 71: cmd.ExecuteNonQuery()
Line 72: con.Close()


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 70

Stack Trace:


[OleDbException (0x80004005):
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) +20
System.Data.OleDb.OleDbConnection.InitializeProvider() +57
System.Data.OleDb.OleDbConnection.Open() +203
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:70
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032

----------------------------------------------------------------------------------------------------------------------

Now I also have a discussion forum on my site, which I had to make a minor
adjustement to in order to post the messages to the database, I remember the
original code indicated something similar to your code in that it pointed to
'databases/forumdb.mdb' but I had to change this slightly to read:
'../databases/forumdb.mdb' and that did the trick.

So, logically I tried the same thing with your code and changed the three
references to 'databases/imagedb.mdb' to '../databases/imagedb.mdb'. I went
through the same steps to navigate to the gallery link from my home page and
this time I received the following error message:

----------------------------------------------------------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path. Make
sure that the path name is spelled correctly and that you are connected to
the server on which the file resides.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException:
'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path. Make
sure that the path name is spelled correctly and that you are connected to
the server on which the file resides.

Source Error:


Line 68: pms("@FileData").Value = fileData
Line 69:
Line 70: con.Open()
Line 71: cmd.ExecuteNonQuery()
Line 72: con.Close()


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 70

Stack Trace:


[OleDbException (0x80004005):
'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path.
Make sure that the path name is spelled correctly and that you are connected
to the server on which the file resides.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) +20
System.Data.OleDb.OleDbConnection.InitializeProvider() +57
System.Data.OleDb.OleDbConnection.Open() +203
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:70
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032

-----------------------------------------------------------------------------------------------------------------------

Now if you look carefully at the line at the top which references the
directory path, you will notice it now points to:

'd:\websites\swin644498\www\databases\imagedb.mdb' - instead of
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb'

Which indicates I am making progress by adding the ../ to the begining of
the database reference line, but because it's still pointing to
'www\databases' instead of just 'databases' I tried to go back one more
level by changing the reference to the following:

'../../databases/forumdb.mdb'

Again I navigated to the gallery link from my home page and went through the
steps to select a file and click the upload button, but this time the
following error was displayed:

-------------------------------------------------------------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

Cannot use a leading .. to exit above the top directory.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Cannot use a leading .. to exit
above the top directory.

Source Error:


Line 35: '?article=2003031201&page=1
Line 36: Dim strConnection As String
Line 37: strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
Line 38: "Data Source=" _
Line 39: & Server.MapPath("../../databases/imagedb.mdb")


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 37

Stack Trace:


[HttpException (0x80004005): Cannot use a leading .. to exit above the top
directory.]
System.Web.Util.UrlPath.Reduce(String path) +700
System.Web.Util.UrlPath.Combine(String basepath, String relative) +296
System.Web.HttpRequest.MapPath(String virtualPath, String baseVirtualDir,
Boolean allowCrossAppMapping) +201
System.Web.HttpServerUtility.MapPath(String path) +60
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032

----------------------------------------------------------------------------------------------------------------

And now I am stumped once more, logically I have tried everything I can
think of and again I'm left with a bruised forehead after banging against
the wall too many times, the difference this time is not only have I started
to go gray but my hair is actually falling out now.

PLEASE HELP !!!!

Thanks in advance for any help you may be able to offer.

Kind Regards,
Wayne







Ken Cox said:
Hi Wayne,

Okay, here's the code that should get you going. You'll need an Access
database (called imagedb.mdb) in the App_Data folder with this schema:

id: AutoNumber
FileName:Text
FileSize: Number (long integer)
ContentType: Text
FileData: OLE Object

There are two ASP.NET files. Upload.aspx handles the upload and display.
Imagefetch.aspx is a helper page that provides the image to the datagrid.

This is rough code - barebones stuff with no error checking to speak of.
Most of the code is ripped off from other articles, especially from

File Uploading to Access Database using ASP.NET
by Faisal Khan.

http://www.stardeveloper.com/articles/display.html?article=2003031201&page=1

Anyway, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]

-- upload.aspx
<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnDisplay_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)

' Gets the image data and displays it
' in the datagrid
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
Dim con As New Data.OleDb.OleDbConnection(strConnection)
Dim cmdtext As String = "SELECT ID, FileName," _
& " FileSize, ContentType FROM Files"
Dim cmd As New Data.OleDb.OleDbCommand(cmdtext, con)
Dim ds As New Data.DataSet
Dim da As New Data.OleDb.OleDbDataAdapter _
(cmdtext, strConnection)
da.Fill(ds)
dg2.DataSource = ds
dg2.DataBind()
End Sub


Protected Sub btnUpload_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
' Puts uploaded file in Access database
' Adapted for VB by Ken Cox [MVP] from
' File Uploading to Access Database using ASP.NET
' by Faisal Khan.
' http://www.stardeveloper.com/articles/display.html
'?article=2003031201&page=1
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
If Request.Files.Count > 0 Then
Dim files As HttpFileCollection
Dim afile As HttpPostedFile
files = Request.Files
afile = files(0)
Dim contentType As String = afile.ContentType
Dim filename As String = ""
Dim filelength As Integer = afile.ContentLength
Dim fileData As Byte() = New Byte(filelength) {}
Dim lastpos As Integer
lastpos = afile.FileName.LastIndexOf("\")
filename = afile.FileName.Substring(lastpos + 1)
afile.InputStream.Read(fileData, 0, filelength)
Dim con As New Data.OleDb.OleDbConnection(strConnection)
Dim cmdtext As String = "INSERT INTO Files(FileName," & _
" FileSize, ContentType, FileData) VALUES " & _
" (@FileName, @FileSize, @ContentType, @FileData)"
Dim cmd As New Data.OleDb.OleDbCommand(cmdtext, con)
Dim pms As Data.OleDb.OleDbParameterCollection
pms = cmd.Parameters
pms.Add("@FileName", Data.OleDb.OleDbType.VarChar, 50)
pms.Add("@FileSize", Data.OleDb.OleDbType.Integer)
pms.Add("@ContentType", Data.OleDb.OleDbType.VarChar, 50)
pms.Add("@FileData", Data.OleDb.OleDbType.VarBinary)

pms("@FileName").Value = filename
pms("@FileSize").Value = filelength
pms("@ContentType").Value = contentType
pms("@FileData").Value = fileData

con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload and View Images in MS Access</title>
</head>
<body>
<form id="Form1" runat="server" enctype="multipart/form-data">
<input name="thefile" type="file"><br>
<br />
<asp:button id="btnUpload" runat="server" text="Upload"
onclick="btnUpload_Click" />&nbsp;<br />
<br />
<asp:button id="btnDisplay" runat="server"
onclick="btnDisplay_Click" text="View Images" />&nbsp;<br />
<br />
<asp:datagrid id="dg2" runat="server" autogeneratecolumns="False">
<columns>
<asp:boundcolumn datafield="filename" headertext="Filename"
readonly="True"></asp:boundcolumn>
<asp:boundcolumn datafield="Filesize" headertext="Filesize"
readonly="True"></asp:boundcolumn>
<asp:boundcolumn datafield="contenttype"
headertext="Contenttype" readonly="True"></asp:boundcolumn>
<asp:templatecolumn headertext="Image">
<itemtemplate>
<asp:image runat="server" imageurl='<%#
"imagefetch.aspx?id=" & DataBinder.Eval(Container, "DataItem.id") %>' />
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<br />
</form>
</body>
</html>

-- imagefetch.aspx

<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
' Gets the image from the Access
' Database for inclusion in another page
' Adapted from an MS KB article by Ken Cox [MVP]
'
Dim recno As Integer
If Request("id") = "" Then
Exit Sub
End If
recno = Request("id")
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
Dim con As New Data.OleDb.OleDbConnection _
(strConnection)
Dim da As New Data.OleDb.OleDbDataAdapter _
("Select filedata From files where id=" & _
recno.ToString, con)
Dim MyCB As New Data.OleDb.OleDbCommandBuilder(da)
Dim ds As New Data.DataSet()
con.Open()
da.Fill(ds, "images")
Dim myRow As Data.DataRow
myRow = ds.Tables("images").Rows(0)
Dim MyData() As Byte
MyData = myRow("filedata")
Response.Buffer = True
Response.ContentType = "Image/JPEG"
Response.BinaryWrite(MyData)
MyCB = Nothing
ds = Nothing
da = Nothing
con.Close()
con = Nothing
End Sub
</script>



Wayne Smith said:
I've come up against a major headache that I can't seem to find a
solution for but I'm sure there must be a workaround and I would really
be grateful of any help.

I'm currently building a web site for a small club I belong to and one of
the features I would like to include is the ability to allow users to
upload image files.

unfortunately the servers web root www folder only allows READ and
EXECUTE permissions, which makes it impossible to allow a user to upload
a gif/jpeg image. When I FTP into the site however, I can immediately see
two folders:

databases
www

the www folder is the root folder and all web pages need to be placed
there, but the databases folder does have Read, WRITE and Execute
permissions and I have been reliably informed that some ASP.NET code
would allow a user to send a picture file to an Access database in this
folder, the problem I have is not knowing how to reference it because
it's not directly accessible from the Internet - i.e.
www.website.co.uk/databases would not work.

The server is running ASP.NET 1.1 and the permissions setup has to stay
the way it is, I'm not able to get my host to change the permissions on
the root folder so I have to try and make use of the databases folder
instead. For arguments sake lets also assume I already have a simple
Access database called images.mdb with one table called Images, which in
turn contains two fields, one called ID (Autonumber) (Primary Key) and
another called images (OLE Object)

What I would really like, if possible, is a very simple sample page with
a text box and a Browse button, with a second button to 'Upload' the file
once selected, plus any supporting pages that may be required to complete
the operation, i.e.:

selectfile.aspx (simple page to select image file and send to database)
uploadfile.aspx (works behind the scenes to transfer the file)
confirmation.aspx (a confirmation page to indicate the transfer either
succeeded or failed)

I'm not a natural programmer by any stretch of the imagination, which is
why I'm being a bit cheeky and asking for a very simple layout to
accomplish this task, or a straight forward dummies tutorial which takes
into consideration the problem with uploading to a database outside of
the root web.

If anybody can help me solve this problem, I will forever be indebted to
you. I have searched the Internet for over a week and remarkably I cannot
find an answer to this problem, it's now driving me nuts - I just want to
solve this so I can move on with the rest of the site design.

Any help would be greatly appreciated.

Many thanks in advance
Wayne
 
K

Ken Cox [Microsoft MVP]

Hi Wayne,

It looks like your databases folder is outside the root of your web
application and needs to be changed in all the code.

You'll have to experiment with the path *without* using server.mappatth.

It might end up being something like this:

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=d:\websites\swin644498\databases\imagedb.mdb"

or like this:

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=../databases/imagedb.mdb"

or even

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=../../databases/imagedb.mdb"

Keep trying, you're nearly there!

Ken
Microsoft MVP [ASP.NET]


Wayne Smith said:
Hi Ken,
Many thanks for taking the time to post your response, it really is
appreciated.

I followed your instructions pretty much to the letter but I'm still
coming up with an error message, I'll try and be as detailed as I can with
what I have done so you may be able to spot something obvious that I can't
see.

I created a database exactly as you said, the only change I made was to
upload it into the 'databases' folder, and not a folder called 'App_Data',
simply because when I FTP into my site the two folders I see are:

databases
www

And I didn't want to change anything with the 'databases' folder as that
currently has the Read, WRITE and Execute permissions, but to be fair that
shouldn't make much difference because I also changed the line of code in
three places which references 'App_Data/imagedb.mdb' to
'databases/imagedb.mdb'

That was the only change I made so I then uploaded the 'imagedb.mdb' file
to the 'databases' folder, and I uploaded both the 'upload.aspx' and
'imagefetch.aspx' files to the www/gallery folder - so far so good.

I then opened the site and navigated to the 'gallery' link from my home
page, the page displayed correctly giving me a text box with a browse
button, and underneath an 'upload' button and a 'view images' button.

I selected a gif file from my desktop and clicked 'upload', after a few
seconds the following error page was displayed:

--------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException:
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.

Source Error:


Line 68: pms("@FileData").Value = fileData
Line 69:
Line 70: con.Open()
Line 71: cmd.ExecuteNonQuery()
Line 72: con.Close()


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 70

Stack Trace:


[OleDbException (0x80004005):
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) +20
System.Data.OleDb.OleDbConnection.InitializeProvider() +57
System.Data.OleDb.OleDbConnection.Open() +203
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:70
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032

----------------------------------------------------------------------------------------------------------------------

Now I also have a discussion forum on my site, which I had to make a minor
adjustement to in order to post the messages to the database, I remember
the original code indicated something similar to your code in that it
pointed to 'databases/forumdb.mdb' but I had to change this slightly to
read: '../databases/forumdb.mdb' and that did the trick.

So, logically I tried the same thing with your code and changed the three
references to 'databases/imagedb.mdb' to '../databases/imagedb.mdb'. I
went through the same steps to navigate to the gallery link from my home
page and this time I received the following error message:

----------------------------------------------------------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path.
Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException:
'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path.
Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.

Source Error:


Line 68: pms("@FileData").Value = fileData
Line 69:
Line 70: con.Open()
Line 71: cmd.ExecuteNonQuery()
Line 72: con.Close()


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 70

Stack Trace:


[OleDbException (0x80004005):
'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path.
Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) +20
System.Data.OleDb.OleDbConnection.InitializeProvider() +57
System.Data.OleDb.OleDbConnection.Open() +203
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:70
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032

-----------------------------------------------------------------------------------------------------------------------

Now if you look carefully at the line at the top which references the
directory path, you will notice it now points to:

'd:\websites\swin644498\www\databases\imagedb.mdb' - instead of
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb'

Which indicates I am making progress by adding the ../ to the begining of
the database reference line, but because it's still pointing to
'www\databases' instead of just 'databases' I tried to go back one more
level by changing the reference to the following:

'../../databases/forumdb.mdb'

Again I navigated to the gallery link from my home page and went through
the steps to select a file and click the upload button, but this time the
following error was displayed:

-------------------------------------------------------------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

Cannot use a leading .. to exit above the top directory.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Cannot use a leading .. to
exit above the top directory.

Source Error:


Line 35: '?article=2003031201&page=1
Line 36: Dim strConnection As String
Line 37: strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
Line 38: "Data Source=" _
Line 39: & Server.MapPath("../../databases/imagedb.mdb")


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 37

Stack Trace:


[HttpException (0x80004005): Cannot use a leading .. to exit above the top
directory.]
System.Web.Util.UrlPath.Reduce(String path) +700
System.Web.Util.UrlPath.Combine(String basepath, String relative) +296
System.Web.HttpRequest.MapPath(String virtualPath, String
baseVirtualDir, Boolean allowCrossAppMapping) +201
System.Web.HttpServerUtility.MapPath(String path) +60
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032

----------------------------------------------------------------------------------------------------------------

And now I am stumped once more, logically I have tried everything I can
think of and again I'm left with a bruised forehead after banging against
the wall too many times, the difference this time is not only have I
started to go gray but my hair is actually falling out now.

PLEASE HELP !!!!

Thanks in advance for any help you may be able to offer.

Kind Regards,
Wayne







Ken Cox said:
Hi Wayne,

Okay, here's the code that should get you going. You'll need an Access
database (called imagedb.mdb) in the App_Data folder with this schema:

id: AutoNumber
FileName:Text
FileSize: Number (long integer)
ContentType: Text
FileData: OLE Object

There are two ASP.NET files. Upload.aspx handles the upload and display.
Imagefetch.aspx is a helper page that provides the image to the datagrid.

This is rough code - barebones stuff with no error checking to speak of.
Most of the code is ripped off from other articles, especially from

File Uploading to Access Database using ASP.NET
by Faisal Khan.

http://www.stardeveloper.com/articles/display.html?article=2003031201&page=1

Anyway, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]

-- upload.aspx
<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnDisplay_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)

' Gets the image data and displays it
' in the datagrid
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
Dim con As New Data.OleDb.OleDbConnection(strConnection)
Dim cmdtext As String = "SELECT ID, FileName," _
& " FileSize, ContentType FROM Files"
Dim cmd As New Data.OleDb.OleDbCommand(cmdtext, con)
Dim ds As New Data.DataSet
Dim da As New Data.OleDb.OleDbDataAdapter _
(cmdtext, strConnection)
da.Fill(ds)
dg2.DataSource = ds
dg2.DataBind()
End Sub


Protected Sub btnUpload_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
' Puts uploaded file in Access database
' Adapted for VB by Ken Cox [MVP] from
' File Uploading to Access Database using ASP.NET
' by Faisal Khan.
' http://www.stardeveloper.com/articles/display.html
'?article=2003031201&page=1
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
If Request.Files.Count > 0 Then
Dim files As HttpFileCollection
Dim afile As HttpPostedFile
files = Request.Files
afile = files(0)
Dim contentType As String = afile.ContentType
Dim filename As String = ""
Dim filelength As Integer = afile.ContentLength
Dim fileData As Byte() = New Byte(filelength) {}
Dim lastpos As Integer
lastpos = afile.FileName.LastIndexOf("\")
filename = afile.FileName.Substring(lastpos + 1)
afile.InputStream.Read(fileData, 0, filelength)
Dim con As New Data.OleDb.OleDbConnection(strConnection)
Dim cmdtext As String = "INSERT INTO Files(FileName," & _
" FileSize, ContentType, FileData) VALUES " & _
" (@FileName, @FileSize, @ContentType, @FileData)"
Dim cmd As New Data.OleDb.OleDbCommand(cmdtext, con)
Dim pms As Data.OleDb.OleDbParameterCollection
pms = cmd.Parameters
pms.Add("@FileName", Data.OleDb.OleDbType.VarChar, 50)
pms.Add("@FileSize", Data.OleDb.OleDbType.Integer)
pms.Add("@ContentType", Data.OleDb.OleDbType.VarChar, 50)
pms.Add("@FileData", Data.OleDb.OleDbType.VarBinary)

pms("@FileName").Value = filename
pms("@FileSize").Value = filelength
pms("@ContentType").Value = contentType
pms("@FileData").Value = fileData

con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload and View Images in MS Access</title>
</head>
<body>
<form id="Form1" runat="server" enctype="multipart/form-data">
<input name="thefile" type="file"><br>
<br />
<asp:button id="btnUpload" runat="server" text="Upload"
onclick="btnUpload_Click" />&nbsp;<br />
<br />
<asp:button id="btnDisplay" runat="server"
onclick="btnDisplay_Click" text="View Images" />&nbsp;<br />
<br />
<asp:datagrid id="dg2" runat="server" autogeneratecolumns="False">
<columns>
<asp:boundcolumn datafield="filename"
headertext="Filename" readonly="True"></asp:boundcolumn>
<asp:boundcolumn datafield="Filesize"
headertext="Filesize" readonly="True"></asp:boundcolumn>
<asp:boundcolumn datafield="contenttype"
headertext="Contenttype" readonly="True"></asp:boundcolumn>
<asp:templatecolumn headertext="Image">
<itemtemplate>
<asp:image runat="server" imageurl='<%#
"imagefetch.aspx?id=" & DataBinder.Eval(Container, "DataItem.id") %>' />
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<br />
</form>
</body>
</html>

-- imagefetch.aspx

<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
' Gets the image from the Access
' Database for inclusion in another page
' Adapted from an MS KB article by Ken Cox [MVP]
'
Dim recno As Integer
If Request("id") = "" Then
Exit Sub
End If
recno = Request("id")
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
Dim con As New Data.OleDb.OleDbConnection _
(strConnection)
Dim da As New Data.OleDb.OleDbDataAdapter _
("Select filedata From files where id=" & _
recno.ToString, con)
Dim MyCB As New Data.OleDb.OleDbCommandBuilder(da)
Dim ds As New Data.DataSet()
con.Open()
da.Fill(ds, "images")
Dim myRow As Data.DataRow
myRow = ds.Tables("images").Rows(0)
Dim MyData() As Byte
MyData = myRow("filedata")
Response.Buffer = True
Response.ContentType = "Image/JPEG"
Response.BinaryWrite(MyData)
MyCB = Nothing
ds = Nothing
da = Nothing
con.Close()
con = Nothing
End Sub
</script>



Wayne Smith said:
I've come up against a major headache that I can't seem to find a
solution for but I'm sure there must be a workaround and I would really
be grateful of any help.

I'm currently building a web site for a small club I belong to and one
of the features I would like to include is the ability to allow users to
upload image files.

unfortunately the servers web root www folder only allows READ and
EXECUTE permissions, which makes it impossible to allow a user to upload
a gif/jpeg image. When I FTP into the site however, I can immediately
see two folders:

databases
www

the www folder is the root folder and all web pages need to be placed
there, but the databases folder does have Read, WRITE and Execute
permissions and I have been reliably informed that some ASP.NET code
would allow a user to send a picture file to an Access database in this
folder, the problem I have is not knowing how to reference it because
it's not directly accessible from the Internet - i.e.
www.website.co.uk/databases would not work.

The server is running ASP.NET 1.1 and the permissions setup has to stay
the way it is, I'm not able to get my host to change the permissions on
the root folder so I have to try and make use of the databases folder
instead. For arguments sake lets also assume I already have a simple
Access database called images.mdb with one table called Images, which in
turn contains two fields, one called ID (Autonumber) (Primary Key) and
another called images (OLE Object)

What I would really like, if possible, is a very simple sample page with
a text box and a Browse button, with a second button to 'Upload' the
file once selected, plus any supporting pages that may be required to
complete the operation, i.e.:

selectfile.aspx (simple page to select image file and send to database)
uploadfile.aspx (works behind the scenes to transfer the file)
confirmation.aspx (a confirmation page to indicate the transfer either
succeeded or failed)

I'm not a natural programmer by any stretch of the imagination, which is
why I'm being a bit cheeky and asking for a very simple layout to
accomplish this task, or a straight forward dummies tutorial which takes
into consideration the problem with uploading to a database outside of
the root web.

If anybody can help me solve this problem, I will forever be indebted to
you. I have searched the Internet for over a week and remarkably I
cannot find an answer to this problem, it's now driving me nuts - I just
want to solve this so I can move on with the rest of the site design.

Any help would be greatly appreciated.

Many thanks in advance
Wayne
 
K

Ken Cox [Microsoft MVP]

BTW, when you upload an image, note that the code assumes that you are using
..jpg.

Ken
 
W

Wayne Smith

Ken,
I have this unusual desire to give you a great big sloppy kiss right now,
and its only the fact that you probably haven't got long blonde hair and a
great pair of legs that I shall refrain for the moment.

As I'm sure you have already guessed, the information you provided has
worked a treat and solved my long suffering problem, I really can't thank
you enough - you're a credit to these news groups.

Thanks again for all of your help Ken, I can't tell you how much I
appreciate your assistance.

Best Regards,
Wayne



Ken Cox said:
Hi Wayne,

It looks like your databases folder is outside the root of your web
application and needs to be changed in all the code.

You'll have to experiment with the path *without* using server.mappatth.

It might end up being something like this:

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=d:\websites\swin644498\databases\imagedb.mdb"

or like this:

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=../databases/imagedb.mdb"

or even

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=../../databases/imagedb.mdb"

Keep trying, you're nearly there!

Ken
Microsoft MVP [ASP.NET]


Wayne Smith said:
Hi Ken,
Many thanks for taking the time to post your response, it really is
appreciated.

I followed your instructions pretty much to the letter but I'm still
coming up with an error message, I'll try and be as detailed as I can
with what I have done so you may be able to spot something obvious that I
can't see.

I created a database exactly as you said, the only change I made was to
upload it into the 'databases' folder, and not a folder called
'App_Data', simply because when I FTP into my site the two folders I see
are:

databases
www

And I didn't want to change anything with the 'databases' folder as that
currently has the Read, WRITE and Execute permissions, but to be fair
that shouldn't make much difference because I also changed the line of
code in three places which references 'App_Data/imagedb.mdb' to
'databases/imagedb.mdb'

That was the only change I made so I then uploaded the 'imagedb.mdb' file
to the 'databases' folder, and I uploaded both the 'upload.aspx' and
'imagefetch.aspx' files to the www/gallery folder - so far so good.

I then opened the site and navigated to the 'gallery' link from my home
page, the page displayed correctly giving me a text box with a browse
button, and underneath an 'upload' button and a 'view images' button.

I selected a gif file from my desktop and clicked 'upload', after a few
seconds the following error page was displayed:

--------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException:
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.

Source Error:


Line 68: pms("@FileData").Value = fileData
Line 69:
Line 70: con.Open()
Line 71: cmd.ExecuteNonQuery()
Line 72: con.Close()


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 70

Stack Trace:


[OleDbException (0x80004005):
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) +20
System.Data.OleDb.OleDbConnection.InitializeProvider() +57
System.Data.OleDb.OleDbConnection.Open() +203
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:70
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032

----------------------------------------------------------------------------------------------------------------------

Now I also have a discussion forum on my site, which I had to make a
minor adjustement to in order to post the messages to the database, I
remember the original code indicated something similar to your code in
that it pointed to 'databases/forumdb.mdb' but I had to change this
slightly to read: '../databases/forumdb.mdb' and that did the trick.

So, logically I tried the same thing with your code and changed the three
references to 'databases/imagedb.mdb' to '../databases/imagedb.mdb'. I
went through the same steps to navigate to the gallery link from my home
page and this time I received the following error message:

----------------------------------------------------------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path.
Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException:
'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path.
Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.

Source Error:


Line 68: pms("@FileData").Value = fileData
Line 69:
Line 70: con.Open()
Line 71: cmd.ExecuteNonQuery()
Line 72: con.Close()


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 70

Stack Trace:


[OleDbException (0x80004005):
'd:\websites\swin644498\www\databases\imagedb.mdb' is not a valid path.
Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.]
System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) +20
System.Data.OleDb.OleDbConnection.InitializeProvider() +57
System.Data.OleDb.OleDbConnection.Open() +203
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:70
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032

-----------------------------------------------------------------------------------------------------------------------

Now if you look carefully at the line at the top which references the
directory path, you will notice it now points to:

'd:\websites\swin644498\www\databases\imagedb.mdb' - instead of
'd:\websites\swin644498\www\gallery\databases\imagedb.mdb'

Which indicates I am making progress by adding the ../ to the begining of
the database reference line, but because it's still pointing to
'www\databases' instead of just 'databases' I tried to go back one more
level by changing the reference to the following:

'../../databases/forumdb.mdb'

Again I navigated to the gallery link from my home page and went through
the steps to select a file and click the upload button, but this time the
following error was displayed:

-------------------------------------------------------------------------------------------------------------------------


Server Error in '/' Application.
--------------------------------------------------------------------------------

Cannot use a leading .. to exit above the top directory.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Cannot use a leading .. to
exit above the top directory.

Source Error:


Line 35: '?article=2003031201&page=1
Line 36: Dim strConnection As String
Line 37: strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
Line 38: "Data Source=" _
Line 39: & Server.MapPath("../../databases/imagedb.mdb")


Source File: d:\websites\swin644498\www\gallery\upload.aspx Line: 37

Stack Trace:


[HttpException (0x80004005): Cannot use a leading .. to exit above the
top directory.]
System.Web.Util.UrlPath.Reduce(String path) +700
System.Web.Util.UrlPath.Combine(String basepath, String relative) +296
System.Web.HttpRequest.MapPath(String virtualPath, String
baseVirtualDir, Boolean allowCrossAppMapping) +201
System.Web.HttpServerUtility.MapPath(String path) +60
ASP.upload_aspx.btnUpload_Click(Object sender, EventArgs e) in
d:\websites\swin644498\www\gallery\upload.aspx:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032

----------------------------------------------------------------------------------------------------------------

And now I am stumped once more, logically I have tried everything I can
think of and again I'm left with a bruised forehead after banging against
the wall too many times, the difference this time is not only have I
started to go gray but my hair is actually falling out now.

PLEASE HELP !!!!

Thanks in advance for any help you may be able to offer.

Kind Regards,
Wayne







Ken Cox said:
Hi Wayne,

Okay, here's the code that should get you going. You'll need an Access
database (called imagedb.mdb) in the App_Data folder with this schema:

id: AutoNumber
FileName:Text
FileSize: Number (long integer)
ContentType: Text
FileData: OLE Object

There are two ASP.NET files. Upload.aspx handles the upload and display.
Imagefetch.aspx is a helper page that provides the image to the
datagrid.

This is rough code - barebones stuff with no error checking to speak of.
Most of the code is ripped off from other articles, especially from

File Uploading to Access Database using ASP.NET
by Faisal Khan.

http://www.stardeveloper.com/articles/display.html?article=2003031201&page=1

Anyway, let us know if it helps?

Ken
Microsoft MVP [ASP.NET]

-- upload.aspx
<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnDisplay_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)

' Gets the image data and displays it
' in the datagrid
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
Dim con As New Data.OleDb.OleDbConnection(strConnection)
Dim cmdtext As String = "SELECT ID, FileName," _
& " FileSize, ContentType FROM Files"
Dim cmd As New Data.OleDb.OleDbCommand(cmdtext, con)
Dim ds As New Data.DataSet
Dim da As New Data.OleDb.OleDbDataAdapter _
(cmdtext, strConnection)
da.Fill(ds)
dg2.DataSource = ds
dg2.DataBind()
End Sub


Protected Sub btnUpload_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
' Puts uploaded file in Access database
' Adapted for VB by Ken Cox [MVP] from
' File Uploading to Access Database using ASP.NET
' by Faisal Khan.
' http://www.stardeveloper.com/articles/display.html
'?article=2003031201&page=1
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
If Request.Files.Count > 0 Then
Dim files As HttpFileCollection
Dim afile As HttpPostedFile
files = Request.Files
afile = files(0)
Dim contentType As String = afile.ContentType
Dim filename As String = ""
Dim filelength As Integer = afile.ContentLength
Dim fileData As Byte() = New Byte(filelength) {}
Dim lastpos As Integer
lastpos = afile.FileName.LastIndexOf("\")
filename = afile.FileName.Substring(lastpos + 1)
afile.InputStream.Read(fileData, 0, filelength)
Dim con As New Data.OleDb.OleDbConnection(strConnection)
Dim cmdtext As String = "INSERT INTO Files(FileName," & _
" FileSize, ContentType, FileData) VALUES " & _
" (@FileName, @FileSize, @ContentType, @FileData)"
Dim cmd As New Data.OleDb.OleDbCommand(cmdtext, con)
Dim pms As Data.OleDb.OleDbParameterCollection
pms = cmd.Parameters
pms.Add("@FileName", Data.OleDb.OleDbType.VarChar, 50)
pms.Add("@FileSize", Data.OleDb.OleDbType.Integer)
pms.Add("@ContentType", Data.OleDb.OleDbType.VarChar, 50)
pms.Add("@FileData", Data.OleDb.OleDbType.VarBinary)

pms("@FileName").Value = filename
pms("@FileSize").Value = filelength
pms("@ContentType").Value = contentType
pms("@FileData").Value = fileData

con.Open()
cmd.ExecuteNonQuery()
con.Close()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload and View Images in MS Access</title>
</head>
<body>
<form id="Form1" runat="server" enctype="multipart/form-data">
<input name="thefile" type="file"><br>
<br />
<asp:button id="btnUpload" runat="server" text="Upload"
onclick="btnUpload_Click" />&nbsp;<br />
<br />
<asp:button id="btnDisplay" runat="server"
onclick="btnDisplay_Click" text="View Images" />&nbsp;<br />
<br />
<asp:datagrid id="dg2" runat="server"
autogeneratecolumns="False">
<columns>
<asp:boundcolumn datafield="filename"
headertext="Filename" readonly="True"></asp:boundcolumn>
<asp:boundcolumn datafield="Filesize"
headertext="Filesize" readonly="True"></asp:boundcolumn>
<asp:boundcolumn datafield="contenttype"
headertext="Contenttype" readonly="True"></asp:boundcolumn>
<asp:templatecolumn headertext="Image">
<itemtemplate>
<asp:image runat="server" imageurl='<%#
"imagefetch.aspx?id=" & DataBinder.Eval(Container, "DataItem.id") %>' />
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<br />
</form>
</body>
</html>

-- imagefetch.aspx

<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
' Gets the image from the Access
' Database for inclusion in another page
' Adapted from an MS KB article by Ken Cox [MVP]
'
Dim recno As Integer
If Request("id") = "" Then
Exit Sub
End If
recno = Request("id")
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" _
& Server.MapPath("App_Data/imagedb.mdb")
Dim con As New Data.OleDb.OleDbConnection _
(strConnection)
Dim da As New Data.OleDb.OleDbDataAdapter _
("Select filedata From files where id=" & _
recno.ToString, con)
Dim MyCB As New Data.OleDb.OleDbCommandBuilder(da)
Dim ds As New Data.DataSet()
con.Open()
da.Fill(ds, "images")
Dim myRow As Data.DataRow
myRow = ds.Tables("images").Rows(0)
Dim MyData() As Byte
MyData = myRow("filedata")
Response.Buffer = True
Response.ContentType = "Image/JPEG"
Response.BinaryWrite(MyData)
MyCB = Nothing
ds = Nothing
da = Nothing
con.Close()
con = Nothing
End Sub
</script>



I've come up against a major headache that I can't seem to find a
solution for but I'm sure there must be a workaround and I would really
be grateful of any help.

I'm currently building a web site for a small club I belong to and one
of the features I would like to include is the ability to allow users
to upload image files.

unfortunately the servers web root www folder only allows READ and
EXECUTE permissions, which makes it impossible to allow a user to
upload a gif/jpeg image. When I FTP into the site however, I can
immediately see two folders:

databases
www

the www folder is the root folder and all web pages need to be placed
there, but the databases folder does have Read, WRITE and Execute
permissions and I have been reliably informed that some ASP.NET code
would allow a user to send a picture file to an Access database in this
folder, the problem I have is not knowing how to reference it because
it's not directly accessible from the Internet - i.e.
www.website.co.uk/databases would not work.

The server is running ASP.NET 1.1 and the permissions setup has to stay
the way it is, I'm not able to get my host to change the permissions on
the root folder so I have to try and make use of the databases folder
instead. For arguments sake lets also assume I already have a simple
Access database called images.mdb with one table called Images, which
in turn contains two fields, one called ID (Autonumber) (Primary Key)
and another called images (OLE Object)

What I would really like, if possible, is a very simple sample page
with a text box and a Browse button, with a second button to 'Upload'
the file once selected, plus any supporting pages that may be required
to complete the operation, i.e.:

selectfile.aspx (simple page to select image file and send to database)
uploadfile.aspx (works behind the scenes to transfer the file)
confirmation.aspx (a confirmation page to indicate the transfer either
succeeded or failed)

I'm not a natural programmer by any stretch of the imagination, which
is why I'm being a bit cheeky and asking for a very simple layout to
accomplish this task, or a straight forward dummies tutorial which
takes into consideration the problem with uploading to a database
outside of the root web.

If anybody can help me solve this problem, I will forever be indebted
to you. I have searched the Internet for over a week and remarkably I
cannot find an answer to this problem, it's now driving me nuts - I
just want to solve this so I can move on with the rest of the site
design.

Any help would be greatly appreciated.

Many thanks in advance
Wayne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top