Web app download files stored in BLOB

Hi all,

I would like to allow my web app users to download files which are stored in BLOB. I have a WebListBox which shows the filenames that are available for download.
When the user clicks the download button, I tried to write the corresponding file from database to a temporary location for download. The question is, I can’t create the file as it always prompt me UnsupportedFormatException at new FolderItem. My code is as follow:

iif DataList.ListIndex >= 0 then
    dim rs as RecordSet
    rs = session.LocDB.GetRecord("CaseAttachment", DataList.RowTag(DataList.ListIndex))
    if rs <> nil then
      dim FileData As String
      dim TargetFolder, FileName as string
      dim fTarget as FolderItem
      
      FileData = rs.Field("Attachment").NativeValue
      TargetFolder = "c:\\temp\"
      FileName = TargetFolder + DataList.Cell(DataList.ListIndex, 0)
      fTarget = new FolderItem(FileName)
      
      dim bs as BinaryStream = BinaryStream.Create(fTarget, true)
      if bs <> nil then
        bs.write(FileData)
        bs.close
      end if
    end if
  end if

The same code I copied from my Windows app works fine. Please help.

Tony

I figured out that the problem comes from if I assigned a variable name to new FolderItem (still can’t be solved):

      TargetFolder = "c:\\temp\"
      FileName = TargetFolder + rs.Field("FileName").StringValue  //or from the datalist
      fTarget = new FolderItem(FileName)  //this causes exception
    

however, if I hard coded the file name, it works fine:

      TargetFolder = "c:\\temp\"
      FileName = TargetFolder + "test.jpg"
      fTarget = new FolderItem(FileName)

I think you have problem with the character coding. or not

have you checked the content of the variable (FileName) ?

[quote=318128:@Roland Maszlag]I think you have problem with the character coding. or not

have you checked the content of the variable (FileName) ?[/quote]

Yes, it is correct (or it is the same as I expect)

Maybe the there are two different formats? like jpeg and jpg, with orientation informations? i had similar problems when i sent images from ios to webservice to database and then to client pc.

What does the debugger show as the file path that is in your Filename variable when you get the exception?

The docs for the FolderItem constructor say that it will raise an UnsupportedFormatException if the path cannot be resolved. You also are not supplying the second parameter to the constructor to specify the path type, which should probably be PathTypeNative or PathTypeShell.

Hi Paul,

The debugger shows exactly what I expect, here is the entire code:

  if DataList.ListIndex >= 0 then
    dim rs as RecordSet
    rs = session.LocDB.GetRecord("CaseAttachment", DataList.RowTag(DataList.ListIndex))  // GetRecord is my own defined method
    if rs <> nil then
      dim name, FileData As String
      dim TargetFolder, FileName as string
      dim fTarget as FolderItem
      
      FileData = rs.Field("Attachment").NativeValue
      TargetFolder = "c:\\temp\"
      FileName = TargetFolder + rs.Field("FileName").StringValue
      fTarget = new FolderItem(FileName, FolderItem.PathTypeShell)  // this works now after I specify the Path Type
      
      dim bs as BinaryStream = BinaryStream.Create(fTarget, true)
      if bs <> nil then
        bs.write(FileData)
        bs.close
        
        Dim f As new Folderitem(FileName, FolderItem.PathTypeShell)  // Here is where I got the exception
        If f <> nil and f.Exists Then
          DownloadFile = new WebFile
          DownloadFile = WebFile.Open(f)
          DownloadFile.ForceDownload = True
          ShowURL(DownloadFile.URL)
        End If
        
      end if
    end if
  end if
  

You can see that I am trying to save a temp file from database, the file name is stored in the database also so it could not be wrong. If I replace the variable FileName at the place where I got the exception, for instance “c:\temp\test.jpg”, it works perfectly fine. Any ideas?

Tony

Just out of curiosity, what is the filename that’s giving you trouble? It could be an encoding issue.

If the files come from a Xojo app, it’s probably UTF8, so you could try this:

FileName = DefineEncoding(FileName, Encodings.UTF8)

Right after you retrieve the name from the database.

Hi Greg,

Thanks for your suggestion but it doesn’t seem to work either.

The latest version of my app is now I can download the file through explorer by specifying the PathTypeShell, but there are a lot of hypens attachment at the end of the filename, for instance, 6R.jpg-----------------------------------------

When I tried to name the download file using DownloadFile.FileName = DefineEncoding(rs.Field(“FileName”).StringValue, Encodings.UTF8)
it gives me an exception at the browser:
Could not execute returned javascript: Failed to set the ‘location’ property on ‘Window’: '/C5B743FE5228355C636E6C4843A82D4B216DC282/files/4843-1520-3030-6937-7485/6R.jpg%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%

Please help.