SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can you 'reset' Response.ContentType ??

    My client wants to extract records by country from an Excel spreadsheet.
    So I have one script which reloads itself twice :-
    phase 1. display initial form, user select s country and Submits
    form action reloads itself with querystring '...?Extract=YES'

    phase 2. Extract=YES
    the extract code contains the following
    Response.Buffer = true
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader "Content-Disposition", "attachment; filename=Extract_List_" &myCountry &".xls"
    ...
    ...
    Response.Flush
    and stores a record count/message in session variables
    now somehow reload script with querystring '...?Extract=DONE'

    phase 3. Extract=DONE
    display record count/message in session variables using Response.write

    The actual extract works fine, creates the Excel file, but then stops/hangs ??
    It doesn't reload and perform phase 3.

    From 100s tests and various answers via Google, I've gathered that it's the RESPONSE method that I'm not understanding or using correctly. I've tried :
    Response.Redirect ("scriptname?Extract=DONE")
    Response.AddHeader "Refresh", "0;URL=scriptname?Extract=DONE"
    Server.Transfer doesn't accept the ?Extract=DONE
    even a javascript window.location.replace("scriptname?Extract=DONE") and
    window.location.href = "scriptname?Extract=DONE"
    but nothing happens

    Is there a way to 'reset' the Response.ContentType and Headers?, so I can Response.write my record count/message ??

    Can anyone dig me out of this hole ??
    Thanks in advance

  2. #2
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,251
    Mentioned
    113 Post(s)
    Tagged
    1 Thread(s)
    If you've got the content type set to something other than html, I don't think you can use the redirects (but it's been a while since I've done anything like that).

    What I've always done is just create the file on the server, then provided a link to the generated file. The benefit here is you're always sure what has been provided to the user AND the file can be reaccessed at a later date if needed. A csv file will work just as well as an excel spreadsheet unless he wants it formatted or something.

    Code:
    Set objFS = CreateObject("Scripting.FileSystemObject")
    fileName = "excel\text.csv"
    If objFS.FileExists(fileName) Then  ' Check is here just to ensure the file was created....
      Set objFile = objFS.OpenTextFile(fileName, 8) ' 8 is for append
    Else
      Set objFile = objFS.CreateTextFile(fileName, True)
    End If 
    
    For i = 0 to 100
       objFile.Write ("1,2,3,4,5,6,7,8" & CHR(13) & CHR(10))
    Next
    objFile.Close
    
    Response.Write "The generated file is <a href=""excel/text.csv"">here</a>"
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  3. #3
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dave, I've got this working now based on your example with FSO.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •