I am trying to copy files like Word, images, excel etc. into a table. Store them temporarily then have a routine that extracts them back to files when the user requests. I have defined a table with a column called file_data - type - (varbinary(max) to put the blob in.
Here is my File to Blob function. This seems to work for putting the file into the blob column in the table, but for some reason I think only the first 32k is being put in the table. Even though my tests at runtime show that ll_filelen is the proper length of the file I select to import.
li_value = GetFileOpenName("Select File", ls_docname, ls_named, " ", "All Files (*.*), *.*")
ll_FileLen = FileLength64(ls_docname)
If li_value <> 1 Then
Return lblb_temp
end if
li_FileNo = FileOpen(ls_docname, StreamMode!, Read!)
If li_FileNo < 0 Then Return lblb_temp
If ll_FileLen > 32765 Then
If Mod(ll_FileLen, 32765) = 0 Then
li_Reads = ll_FileLen / 32765
Else
li_Reads = (ll_FileLen / 32765) + 1
End If
Else
li_Reads = 1
End If
For li_i = 1 To li_Reads
If FileRead(li_FileNo, lblb_temp) = -1 Then Return lblb_temp
ablb_data = ablb_data + lblb_temp
Next
FileClose(li_FileNo)
string ls_id
long ll_row
dw_2.settrans(sqlca)
ll_row = dw_2.insertrow(0)
ls_id = lf_sp_get_id_num()
is_current_id = ls_id
dw_2.setitem(ll_row, 1, ls_id)
dw_2.setitem(ll_row, 2, ls_named)
dw_2.setitem(ll_row, 3, ls_docname)
dw_2.update()
messagebox('blob len', string(len(ablb_data)))
connect using sqlca;
UpdateBlob EAG_FS_FILE_TRANSFER set file_Data = :ablb_data where file_id = :is_current_id using sqlca;
if sqlca.sqlcode <> 0 then
messagebox("connect error",sqlca.sqlerrtext)
end if
commit;
I then use this function to read the file back out. And it always comes out as just the first 32k of the file. So I am not sure if this is an issue with how I am putting the blob into the table or if this is an issue with how I am reading it out of the table. Anyone see where I messed up?
if dw_2.getrow() > 0 then
li_selected = dw_2.getrow()
ls_file = dw_2.getitemstring(li_selected, 'file_name')
ls_id = dw_2.getitemstring(li_selected, 'file_id')
connect using sqlca;
SELECTBLOB file_data INTO :lb_file FROM EAG_FS_FILE_TRANSFER WHERE file_id = :ls_id;
messagebox('button len', string(len(lb_file)))
if not isnull(lb_file) then
ls_open_path = 'c:\temp\blob\'
if not (fileexists(ls_open_path)) then
createdirectory(ls_open_path)
end if
ls_new_file = ls_open_path + ls_file
if not isnull(lb_file) then
ll_FileLen = Len(lb_file)
messagebox('len', string(ll_filelen))
li_File = FileOpen(ls_new_file, StreamMode!, Write!, LockWrite!, Append!)
If li_File > 0 Then
// Determine how many times to call FileWrite
If ll_FileLen > 32765 Then
If Mod(ll_FileLen, 32765) = 0 Then
ll_Loops = ll_FileLen/32765
Else
ll_Loops = (ll_FileLen/32765) + 1
End If
Else
ll_Loops = 1
End If
For ll_Step = 1 To ll_Loops
FileWrite(li_File,BlobMid(lb_file,((ll_Step - 1)*32765) + 1, 32765))
Next
else
messagebox('not found','')
end if
end if
else
messagebox('Invalid Row', 'select a valid attachment to view')
end if