NilObjectException in HandleSpecialURL

Hello,

I try to download a file to a client using WE/Xojo

But I get “NIL object” errors when I use the following code in app “HandleSpecialURL” (the same code works OK if I put it in a button’s action event)

I have add a Property “W as Webfile” to the session.

[code]dim sFilename as String
dim f as FolderItem
'dim w as WebFile 'moved to Session because w.URL was always empty when declared here

f= GetFolderItem (“c:\test\test.pdf”)

If f <> Nil And f.Exists Then

session.w = new WebFile // NilObjectException
session.w = WebFile.open (f) // NilObjectException here if I remove the above line

session.w.MIMEType = “application/pdf”
session.w.ForceDownload = true

Session.ShowURL (session.w.URL)

end
[/code]

Why does this code result in “NilObjectException” when I run it in HandleSpecialURL ?

Heinz

You can’t use sessions inside HandleSpecialURL as it’s in the App instead of in the Pages and Sessions themselfs.

Use App.SessionWithIdentifier(“SESSIONID”, “IPADDRESS”) to get the session you need

thank you for your reply.

In the meantime I have tried something else, the code I have used is from here:
http://documentation.xojo.com/index.php/WebFile.Download

The description says that I should use it in a Button’s action event… and it works there, but not in “HandleSpecialURL”.

[code] dim textfile as webfile

TextFile = New WebFile // TextFile is a property of the web page
TextFile.MimeType = “text/plain”
TextFile.ForceDownload = True // If False, the browser may try to display the file instead of download it
TextFile.FileName = “TextFile.txt”

TextFile.Data = “Hello, world!”

If TextFile.Download Then // This causes the file to be downloaded
// Download requested
End If

return true[/code]

So how could I download a file using HandleSpecialURL?
The reason why I want to do use HandleSpecialURL is that I want to allow a client to send a filename to download in the URL like this:
http://127.0.0.1:8080/special/get?file=test.pdf

So in HandleSpecialURL I can parse this URL and send the file that the client has requested.

Heinz

Try:

 dim textfile as webfile
  
  TextFile = New WebFile // TextFile is a property of the web page
  TextFile.Session = Nil
  TextFile.MimeType = "text/plain"
  TextFile.ForceDownload = True // If False, the browser may try to display the file instead of download it
  TextFile.FileName = "TextFile.txt"
  
  TextFile.Data = "Hello, world!"
  
  If TextFile.Download Then // This causes the file to be downloaded
    // Download requested
  End If

I’m not sure why it won’t work. But you can also try to use ShowURL(TextFile.URL)

This was one of my first attempts… the result was something like “unknow command” when I’ve tried to compile it.

This I could compile - but didnt work (because session is not available in HandleSpecialURL as you’ve mentioned)

In HandleSpecialURL, data is return to the requester using the Request As WebRequest parameter.

I haven’t tried this, but I suspect you would read in your file as a BinaryStream and then send it back using the Print command:

Request.Print(binaryData) Return True

(you may also need to set the MIME type)

http://documentation.xojo.com/index.php/WebApplication.HandleSpecialURL
http://documentation.xojo.com/index.php/WebRequest

@Derk Jochems
The reason your code doesn’t work is that textfile is going out of scope before the browser gets a chance to grab it. Make that a global property and it’ll probably work.

[quote=16253:@Greg Olone]@Derk Jochems
The reason your code doesn’t work is that textfile is going out of scope before the browser gets a chance to grab it. Make that a global property and it’ll probably work.[/quote]

I know, basicly i wasn’t really understanding muself. As i forgot that the WebRequest handles the response.

Thank you all for your replies.

The following code works with Firefox, but the file is displayed in the browser, no “SaveAsDialog” appears.
With IE9 it doesn’t work at all, the browser doesn’t show anything

[code]dim f as FolderItem
dim mb as MemoryBlock
dim bs as BinaryStream

f = GetFolderItem (“c:\test\test.pdf”)

bs = bs.open (f,False)
mb = bs.Read (bs.Length)

bs.Close

Request.MIMEType = “application/pdf” //neccessary
Request.print mb
[/code]

I have also tried the following, but here nothing happens in Firefox and IE:

[code]dim f as FolderItem
dim mb as MemoryBlock
dim bs as BinaryStream

f = GetFolderItem (“c:\test\test.pdf”)

bs = bs.open (f,False)
mb = bs.Read (bs.Length)

bs.Close

File = New WebFile // TextFile is a global property
File.MimeType = “application/pdf”
File.ForceDownload = True
File.FileName = “Testfile.pdf”
File.Data = mb

If File.Download Then // This causes the file to be downloaded
// Download requested
End If

return true
[/code]

I think that I have found a solution, if I add “Content-Dispostion” header then the browser prompts me with a “Save as” dialog :

request.Header(“Content-Disposition”) = “attachment; filename=test.pdf”