Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 856
  • Last Modified:

Upload a file using a ASP page

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
jsctechy
Asked:
jsctechy
  • 11
  • 6
1 Solution
 
GawaiCommented:
make sure you have set read/write permission on that folder
0
 
GaryCommented:
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
 
jsctechyAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jsctechyAuthor Commented:
>>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
 
GaryCommented:
Replace this...

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


With...

strPath = "C:\Inetpub\wwwroot\ParkRec\PressRelease\pdf\" & strFileName
0
 
jsctechyAuthor Commented:
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
 
GaryCommented:
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
 
jsctechyAuthor Commented:
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
 
GaryCommented:
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
 
jsctechyAuthor Commented:
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
 
GaryCommented:
And you have definitely given the IUSR_machinename Read/Write permissions on that folder?
0
 
jsctechyAuthor Commented:
yes i have done that
0
 
jsctechyAuthor Commented:
ok i got it i was looking at the wrong ASP file so that was causing the error
Thanks
0
 
jsctechyAuthor Commented:
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
 
GaryCommented:
Don't quite follow what you mean? When saving the file?
0
 
jsctechyAuthor Commented:
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
 
jsctechyAuthor Commented:
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
 
jsctechyAuthor Commented:
i already figure it out thanks ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now