Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

Error on script to download files

I have the script below which is used to download files from the server when user clicks on a button.

I have checked the path and it is the correct one on every record yet in this particular page the code doesn't work  :$  ... any ideas ?

Also there is a syntax error on the re-direct page, how can I pass the record ID to the next page ?

This part:   "error.asp?caseid=(Casesjoin.Fields.Item("Id").Value)"

----

 
'Force Download Server Behavior
On Error Resume Next
If Request("Attachments")<>"" Then
            Response.Buffer=true
            File_Path = Request("Attachments")
            Error_Page = "error.asp?caseid=(Casesjoin.Fields.Item("Id").Value)"
            Set FS = Server.CreateObject("ADODB.Stream")
            FS.Type = 1
            FS.Open            
            FS.LoadFromFile(File_Path)
If Err.number = 0 Then
            File_Path = Replace(File_Path, "/", "\")
          if InStr(File_Path, "\") <> 0 Then
                  File_Path = Right(File_Path, Len(File_Path) - InStrRev(File_Path, "\"))
            End If
            File_Name=File_Path
            Response.Clear
            Response.ContentType = "application/octet-stream"
            Response.AddHeader "Content-Disposition", "attachment; filename=" & File_Name
            Response.AddHeader "Content-Transfer-Encoding","binary"
            Response.BinaryWrite FS.Read
            FS.Close
            Response.End
      Else
            Response.Redirect(Error_Page)
      End If
            File_Path = Nothing
            Error_Page = Nothing
End If
 -------

The form that submits the values has the following code:

 <div align="center">
                        <input name="Attachments" type="hidden" id="Attachments" value="<%=Session("Attachmentsfull")%>/<%=(Receipts.Fields.Item("storage_name").Value)%>">
                        <input name="Download" type="submit" class="bodytext" id="Download" value="view">
                      </div>

I checked the session and is correct and so is the storage_name, also the file IS in the correct folder. It simply doesn't download and sends me to the error page when I click on th ebutton to download the file.
Avatar of Big Monty
Big Monty
Flag of United States of America image

first, to fix the syntax error, change your code to:

Error_Page = "error.asp?caseid=" & Casesjoin.Fields.Item("Id").Value

when you say "it doesn't work", can you be more specific? if you comment out or remove the line

On Error Resume Next

does an error occur?
Avatar of Aleks

ASKER

It redirects to the error page, even thought the file is there.

Its weird because I use the exact same code on other pages for other downloads and those work just fine.  I checked the value of the storage_name and the path to the file and they are correct, the file is also there. it simply does not download and redirects to the error page.
Avatar of Aleks

ASKER

Thanks for the syntax ... that part works fine now!
does that still occur with the fix to the Error_Page variable i gave previously?

what if you hard code a value for the path, does that work?
Avatar of Aleks

ASKER

It does occur with the fix of the error, it now does send me to the error page. So .. something is causing the error, hence the redirect.

This is a piece of code someone else did for me, so I am not sure how to hard code it.

But this is an example of the path, perhaps you can help me place it in the code so I can test it.

I hardcoded it like this:

 <div align="center">
                        <input name="Attachments" type="hidden" id="Attachments" value="C:\BDotAttachments\BlueDotDev/2_10789_05262015202949_Birth Certificate.pdf">
                        <input name="Download" type="submit" class="bodytext" id="Download" value="view">
                      </div>

which is the path being used and its correct, if I past that on my browser on a new screen I get the file, but this still redirects me to the error page.
Avatar of Aleks

ASKER

I submitted the form to another page and requested the form value of "Attachments" and shows up empty  :$
ok do you have any other fields on the page with a name of "Attachments"?

is the Attachments field contained in a form?
Avatar of Aleks

ASKER

Never mind .. there was a typo .. this is the problem.

When there is more than one document it looks like it passes the value of one item * the number of documents I have, instead of the actual value of the selected row:

This is the value I get:

C:\BDotAttachments\BlueDotDev/2_10789_05262015202949_Birth Certificate.pdf, C:\BDotAttachments\BlueDotDev/2_10789_05262015202949_Birth Certificate.pdf, C:\BDotAttachments\BlueDotDev/2_10789_05262015202949_Birth Certificate.pdf, C:\BDotAttachments\BlueDotDev/2_10789_05262015202949_Birth Certificate.pdf
Avatar of Aleks

ASKER

Actually that was the hard coded. When I set the value back to what it should be its passing ALL values and not the one selected, that's what is causing the issue:

C:\BDotAttachments\BlueDotDev/2_10789_05262015204507_Welcome letter.txt, C:\BDotAttachments\BlueDotDev/2_10789_05262015202949_Birth Certificate.pdf, C:\BDotAttachments\BlueDotDev/2_10789_05262015202913_Personal Document.DOCX, C:\BDotAttachments\BlueDotDev/2_10789_05262015202424_Welcome letter.txt
Avatar of Aleks

ASKER

There are no other form fields with the same name. For some reason when there is more than one entry It passes the values of ALL records and not just the one I am selecting  :$  which obviously prompts the error and hence the redirect.
so you have multiple fields called "Attachments" on your page? and your goal is to have them all downloaded?
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

I think I found the issue. It was the way the 'form' was placed in the repeat region. It showed only one result once I changed it .. let me test and confirm.

(Had I never hard coded it I would have not noticed)
Avatar of Aleks

ASKER

It worked  :)  thanks for the help.
Avatar of Aleks

ASKER

Thanks !