Xojo.Net.HTTPSocket import file corrupt

I use a httpsocket to import a pdf from my server to a iPhone. I use this code:

Sub getDocument(urlDoc As Text, outputFile As Xojo.IO.FolderItem)

Declare Function CFURLCreateStringByAddingPercentEscapes Lib “Foundation” (allocator As Ptr, origString As CFStringRef, charactersToLeaveUnescaped As CFStringRef, legalURLCharactersToBeEscaped As CFStringRef, encoding As UInt32) As CFStringRef

Dim encodedURL As Text
encodedURL = CFURLCreateStringByAddingPercentEscapes(Nil, urlDoc, Nil, Nil, &h08000100)
// Call to get document
Self.Send(“GET”, encodedURL, outputFile)

End Sub

I have another code in the " FileReceived" of the socket Event to write the imported file on the SpecialFolder.Documents of my app on the mobile device.
The document I import is a PDF file. The importation works fine.

When I open this document in my app, I call adobe acrobat reader. There is always this message that appears:
" Impossible to open this document, It is corrupted".

If I copy myself the same PDF using iTunes on the mobile device into the app, I open it using my app and it works fine.

Is there a problem when the file is imported from the server?

Hi,

What is the httpstatus in the FileReceived event ?
Is file.length consistent with the PDF file size in the FileReceived event ?

How do you call adobe acrobat reader ?
Where is the pdf file stored on the device ?
Are you sure Adobe Acrobat Reader has read access to the file ?

Have you tried displaying the PDF in a iOSHTMLViewer ?

What is the httpstatus in the FileReceived event ? 200 which means success

Is file.length consistent with the PDF file size in the FileReceived event ? No. Original document 44 Ko
Downloaded doc. 41 Ko
Have you tried displaying the PDF in a iOSHTMLViewer ? Yes but did not work.
Where is the pdf file stored on the device ? SpecialFolder.Documents
Are you sure Adobe Acrobat Reader has read access to the file ? Yes

How do you call adobe acrobat reader ?
I used your code from a previous post:

//create the view controller
using Extensions
using Foundation
using UIKit
dim controller as UIActivityViewController
Dim fileArr As NSArray = NSArray.CreateWithObject(NSURL.URLWithString(encodedUrl))
controller = new UIActivityViewController(fileArr, nil)
Dim excludeArr As NSArray = NSArray.CreateWithObject( new NSString(UIActivity.UIActivityTypePrint))
excludeArr = excludeArr.ArrayByAddingObject(new NSString(UIActivity.UIActivityTypeCopyToPasteboard))
excludeArr = excludeArr.ArrayByAddingObject(new NSString(UIActivity.UIActivityTypeMail))
controller.excludedActivityTypes = excludeArr
//present with nil completion handler
self.PresentViewController(controller, True, nil)

Because the length of the file is not the same as the original, I am wondering why it cuts it???

Thanks for help.

The file may not be completely written in the FileReceived event. Try using Xojo.Timer.CallLater to call another method to process the file.

I’ve finally found the bug when I went through the code of Jeremie Leroy in his project example “pdf.test”.

The problem was when the file was received in a temporary file into the FileReceive event. After, there was a copy from the temporary file to the permanent file. This was the code in the FileReceive Event:

Dim repDoc As Xojo.IO.FolderItem = Xojo.IO.SpecialFolder.Documents
Dim doc As Xojo.IO.FolderItem = repDoc.Child(File.Name)
If doc.Exists Then
doc.Delete
End If
File.CopyTo(repDoc)
// Delete document
File.Delete

What I did was to give directly the permanent file in the socket request. It seems probably the copy takes probably a little bit too long and the File.Delete is executed too early.

Thanks for all.