Solved

Upload a file using a ASP page

Posted on 2006-10-31
18
782 Views
Last Modified: 2010-05-18
Hi there,
I have a page that takes the input form the user and allows then to save a file to a folder in the root directory. but for some reason is not working properly i'm using a file i got somewhere called clsField.asp along with my page but what i think is falling is the writtting the new file to  the folder.
Here is the code for my page:
 <%@language=VBScript%>
<!--#include file="../includes/security.asp"-->
<!--   #include file="../includes/dbConnect.asp"  -->
<!--#INCLUDE FILE="../clsUpload.asp"-->
<%
Dim objUpload
Dim strFileName
Dim objConn
Dim objRs
Dim lngFileID

' Instantiate Upload Class
Set objUpload = New clsUpload

' Grab the file name
strFileName = objUpload.Fields("pdf").FileName

Set objConn = Server.CreateObject("ADODB.Connection")
Set objRs = Server.CreateObject("ADODB.Recordset")


objConn.open sConnect


'objConn.Open sConnect

objRs.Open "Releasses", objConn, 3, 3

objRs.AddNew


objRs.Fields("ReleaseName").Value =      objUpload.Fields("ReleaseName").Value
objRs.Fields("PostOnline").Value =       objUpload.Fields("PostOnline").Value

If objUpload.Fields("PostOnline").Value = "True" Then
      objRs.Fields("PostOnline").Value = True
Else
      objRs.Fields("PostOnline").Value = False
End If


objRs.Fields("pdfname").Value = strFileName

' Compile path to save file to
strPath = Server.MapPath("\ParkRec\pdf") & "\" & strFileName


' Save the binary data to the file system
objUpload("pdf").SaveAs strPath

objRs.Update
objRs.Close

Set objRs = Nothing
Set objConn = Nothing
'*********Set objUpload = Nothing

Response.Redirect("NewReleaseDisplay.asp.asp?ri=" & objUpload.Fields("ReleaseName").Value)
%>

and the part where i'm getting the error on the clsField is as follows:

            ' Save the binary data to file system
            '      Overwrites file if previously exists!
            Call lobjStream.SaveToFile(pstrFileName, adSaveCreateOverWrite)

the error i'm getting is:
Error Type:
ADODB.Stream (0x800A0BBC)
Write to file failed.
/ParkRec/clsField.asp, line 178

Can anyone help me resolving this issue?
Thanks
0
Comment
Question by:jsctechy
  • 11
  • 6
18 Comments
 
LVL 20

Expert Comment

by:Gawai
Comment Utility
make sure you have set read/write permission on that folder
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
The last place you should allow uploads to is the root directory - anyone could upload a new index page and bang goes your site, you should use a sub folder that is for uploads only.
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
no i do have a different folder in the root, sorry i did not specified that my root looks like this:
C:\Inetpub\wwwroot\ParkRec\PressRelease\pdf and here is where the files that are upload will be store
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
>>make sure you have set read/write permission on that folder
i did that already and still have a the same error

any suggestion?
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
Replace this...

strPath = Server.MapPath("\ParkRec\pdf") & "\" & strFileName


With...

strPath = "C:\Inetpub\wwwroot\ParkRec\PressRelease\pdf\" & strFileName
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
Error Type:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/ParkRec/PressRelease/NewReleasePost.asp, line 42, column 66
strPath = "C:\Inetpub\wwwroot\ParkRec\PressRelease<WBR>\pdf\" &amp; strFileName
-----------------------------------------------------------------^
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Think something went wrong there with copying, when copying text from this page paste to notepad first to get rid of formatting if you're using FP or similar
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
yeah  i just notice that thanks.

Now for testing purposes this is great because i found the folder and store the file there, but when i'm ready to deploy my application it will save somewhere el rather than the C:\Inetpub\wwwroot\ParkRec\PressRelease\pdf\ how can i make the path change instead of having to write the exact location of where i want it
My guess here is the previous person who code this page that i took as a reference uses:
strPath = Server.MapPath("\ParkRec\pdf") & "\" & strFileName
where somewhere Server.MapPath is define with the exact location how can i use something like that?
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
But is it working now?
Once you deploy you can easily find out the exact path using a response.write server.mappath("...") then hardcode that into your code.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:jsctechy
Comment Utility
oh ok
not is not working it wites it to the database but it does not save the file to the folder because it triggers an error that i have specified
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
And you have definitely given the IUSR_machinename Read/Write permissions on that folder?
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
yes i have done that
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
ok i got it i was looking at the wrong ASP file so that was causing the error
Thanks
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
i just have a question now that the file is getting store in the folder my page has and if statement that if the name already exist to show something else but for some reason is not doing it. Is there a way that you can take a look of it?

<%@language=VBScript%>
<!--#include file="../includes/security.asp"-->
<!--   #include file="../includes/dbConnect.asp"  -->
<html>
<head>
<title>Administration</title>

 
</head>
<%
ReleaseID = Request.QueryString("ri")


'login to the database for details see dbConnect.asp
set conn = server.CreateObject("ADODB.Connection")

conn.open sConnect

mySQL = "select * from Releasses where ReleaseName = '" & ReleaseID & "'"

response.write mySQL
Response.end

set rstemp = conn.Execute(mySQL)
 
if not rstemp.EOF then


%>
<body bgcolor="#ADCED3" text="#000000"  leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<!--#include file="../includes/headerinc.htm"-->

<table width="100%" border="0" cellspacing="0" cellpadding="0" >
<tr>
    <td align="center" valign=top >
    <table width="760" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
    <tr>
      <td width="760" align=center>
          <img src="images/viewupdatedeletereleases.jpg" alt="View New Release" width="307" height="64">
      </td>
         </tr>
         </table>
         </td>
</tr>    
   
<tr>
<td align=center  valign="top" >

    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="760" bgcolor="#FFFFFF">
    <tr>
       <td COLSPAN=3>&nbsp;</td></tr>
    </tr>
    <tr>
       <td COLSPAN=3>&nbsp;</td></tr>
    </tr>
    <TR>
     <TD WIDTH="30"  ALIGN=LEFT>&nbsp;</TD>
     <td width="700" valign=top align=center >

     <form action="NewReleaseDisplay.asp" method="post" name="table1"  >

    <input type="hidden" name="tex" value=5>

    <font face="Verdana, Arial, Helvetica" Size="2">
    <table width="576" border="0" cellspacing="0" cellpadding="2" bgcolor="#FFFFFF" bordercolor="#FFFFFF">
    <tr>
     <td align=center>
      <table width="400" border="0" cellspacing="2" cellpadding="2">
      <tr>
         <td bgcolor="#F0E7B0" width="120" align="right"><font face="Verdana, Arial, Helvetica" Size="2">
         Release Description:</font></td>
         <td bgcolor="#ADCED3" width="260"><font face="Verdana, Arial, Helvetica" Size="2"><% Response.Write rstemp("ReleaseName") %></font></td>
      </tr>
       <tr>
         <td bgcolor="#F0E7B0" width="120" align="right"><font size="2">Post Online</font><font face="Verdana, Arial, Helvetica" Size="2">:</font></td>
         <td bgcolor="#ADCED3" width="260"><font face="Verdana, Arial, Helvetica" Size="2"><% Response.Write rstemp("PostOnline")%></font></td>
      </tr>
      <tr>
           <font face="Verdana, Arial, Helvetica" Size="2">
         <td bgcolor="#F0E7B0" width="120" align="right"><font face="Verdana, Arial, Helvetica" Size="2">
         PDF Name:</font></td>
         <td bgcolor="#ADCED3" width="260"><font face="Verdana, Arial, Helvetica" Size="2"><a href="../PressRelease/pdf/<% Response.Write rstemp("pdfname")%>"><% Response.Write rstemp("pdfname")%></a></font></td>
  </font>
      </tr>
      <tr><td colspan=2>&nbsp;</td></tr>
      <tr><td colspan=2>&nbsp;</td></tr>
      <tr>
       <td colspan=2><font face="Verdana, Arial, Helvetica" Size="2">
       <a href="PressReleasses.asp">Back to Add a New Release</a></font></td>
      </tr>
      <tr><td colspan=2>&nbsp;</td></tr>
      <tr>
       <td colspan=2><font face="Verdana, Arial, Helvetica" Size="2">
                <a href="../user.asp">Back to Administration Menu</a>
 
       </font></td>
      </tr>
      </table>
     </td>
    </tr>
    </table>


  </font>
</form>



<% else %>
<body bgcolor="#ADCED3" text="#000000"  leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" border="0" cellspacing="0" cellpadding="0" >
<tr>
    <td align="center" valign=top >
    <table width="760" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
    <tr>
      <td width="760" align=center>
          <img src="images/viewupdatedeletereleases.jpg" alt="View New Release" width="307" height="64">
      </td>
         </tr>
         </table>
         </td>
</tr>    
   
<tr>
<td align=center  valign="top" >

    <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 width="760" bgcolor="#FFFFFF">
    <tr>
       <td COLSPAN=3>&nbsp;</td></tr>
    </tr>
    <tr>
       <td COLSPAN=3>&nbsp;</td></tr>
    </tr>
    <TR>
     <TD WIDTH="30"  ALIGN=LEFT>&nbsp;</TD>
     <td width="700" valign=top align=center >

    <form action="NewReleaseDisplay.asp" method="post" name="table2"  >

    <input type="hidden" name="tex" value=5>

    <font face="Verdana, Arial, Helvetica" size="2">
   <table width="576" border="0" cellspacing="0" cellpadding="2" >
    <tr>
      <td width="576" align=center>
      <table width="400" border="0" cellspacing="0" cellpadding="2">
      <tr>
         <td><font face="Verdana, Arial, Helvetica" size="2">The Release entered is already taken.<BR>
            Please try again.<BR>
            </td>
         </tr>
         <tr><td>&nbsp;</td></tr>
      <tr><td>&nbsp;</td></tr>
      <tr>
         <td><font face="Verdana, Arial, Helvetica" Size="2">
         <a href="PressReleasses.asp">Back to Add a </a></font>
         <font size="2"><a href="PressReleasses.asp">New Release</a></font></td>
      </tr>
         <tr><td>&nbsp;</td></tr>
      <tr>
         <td><font face="Verdana, Arial, Helvetica" Size="2">
                <a href="../user.asp">Back to Administration Menu</a>
</font></td>
       </tr>
      </table>
      </td>
     </tr>
     </table>
  </font>
</form>
<%end if%>

    </TD>
    <TD WIDTH="30"  ALIGN=LEFT>&nbsp;</TD>
</TR>
<TR>
    <TD WIDTH="30"  ALIGN=LEFT>&nbsp;</TD>
    <TD WIDTH="700" align=left>&nbsp;</td>
    <TD WIDTH="30"  ALIGN=LEFT>&nbsp;</TD>
</tr>
</table>

</td>
</tr>

    <tr>
    <td>
     <div align="center">
      <center>
      <table border="0" cellpadding="0" cellspacing="0" width="760" >
        <tr>
          <td >
                <!--#include file="../includes/footerinc.htm"-->
          </td>
        </tr>
      </table>
      </center>
    </div>
    </td>
    </tr>
</table>

 
 
</body>
</html>

0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Don't quite follow what you mean? When saving the file?
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
if i create a new release with the same name and i attached a pdf file with it it will not show that pdf instead of i will show the first one that was store prior my latest addition is there a way to fix this? or i need to open a new question?
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
i think my error is here: this is my NewReleasePost.asp page, in this file i wil take the inputs from the user and i will store to a SQL database, so i'm grabing the file by the name but in the database the name can be the same. In the database i have a field called ReleaseID where incremnets anytime a new release is added if instead of passing this attribute to my next page instead of the release name it will be great because that field is unique on the table .

here the code for the asp page:

<%@language=VBScript%>
<!--#include file="../includes/security.asp"-->
<!--   #include file="../includes/dbConnect.asp"  -->
<!--#INCLUDE FILE="../clsUpload.asp"-->
<%
Dim objUpload
Dim strFileName
Dim objConn
Dim objRs
Dim lngFileID

' Instantiate Upload Class
Set objUpload = New clsUpload

' Grab the file name
strFileName = objUpload.Fields("pdf").FileName

Set objConn = Server.CreateObject("ADODB.Connection")
Set objRs = Server.CreateObject("ADODB.Recordset")


objConn.open sConnect


objRs.Open "Releasses", objConn, 3, 3

objRs.AddNew

objRs.Fields("ReleaseName").Value =      objUpload.Fields("ReleaseName").Value
objRs.Fields("PostOnline").Value =       objUpload.Fields("PostOnline").Value

If objUpload.Fields("PostOnline").Value = "True" Then
      objRs.Fields("PostOnline").Value = True
Else
      objRs.Fields("PostOnline").Value = False
End If

objRs.Fields("pdfname").Value = strFileName

' Compile path to save file to
strPath = "C:\Inetpub\wwwroot\ParkRec\PressRelease\pdf\" & strFileName
'strPath = Server.MapPath("\ParkRec\pdf") & "\" & strFileName


' Save the binary data to the file system
objUpload("pdf").SaveAs strPath

objRs.Update
objRs.Close

Set objRs = Nothing
Set objConn = Nothing
'*********Set objUpload = Nothing

Response.Redirect("NewReleaseDisplay.asp?ri=" & objUpload.Fields("ReleaseName").Value)
%>

see how my last line will redirect the user ro the a new ASP page base on the ReleaseName store in the DB in line before that.
Is there a way to get the releaseID from the DB and parse instead of releaseName?
Thanks
0
 
LVL 1

Author Comment

by:jsctechy
Comment Utility
i already figure it out thanks ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now