This is a migrated thread and some comments may be shown as answers.

fileupload error file not found

4 Answers 158 Views
Apache Cordova
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Roger
Top achievements
Rank 1
Roger asked on 13 Jan 2014, 06:18 AM
Hi,
am trying to upload a file from an android device (not the emulator).
I'm using the uri of 
var fileURI = fileSys.root.fullPath + '/lightning/attachments/theFile.pdf'
to get

file:///storage/sdcard0/lightning/attachments/theFile.pdf (which exists...I checked).

so if I do 
ft.upload(fileURI,encodeURI('http://myserver'),win,fail)
I get an error code of 1 (File not Found)
I even did a resolveLocalFileSystemURI to get a fileEntry object and use entry.fullPath in the upload function with the same result, but at least I know that the file can be accessed.
Is there some permissions problem or what the heck is going on ????

4 Answers, 1 is accepted

Sort by
0
Accepted
Roger
Top achievements
Rank 1
answered on 14 Jan 2014, 01:20 AM
Hi,
done some more investigation and found that the filetransfer.upload is not accessing my WCF rest service....not even the catch all service...so something wrong here.
The file not found was referring to the website not being found.
Can use Chrome Poster to post a request to the service and it is accepted OK and logs that access has been made. Can also post the file to an aspx page and it is sent OK.
The service is run from a hosted web site so can't put any 3rd party tools on there.
Have scoured the WCF website and can't find any solution....
code is:
        <WebInvoke(UriTemplate:="attachmentSave", Method:="POST", requestFormat:=WebMessageFormat.Json, responseFormat:=WebMessageFormat.Json, bodystyle:=WebMessageBodyStyle.WrappedRequest)>
        Public Function Attachment_Save() As Stream
........................................
End Function

have tried putting 
Public Function Attachment_Save(data as stream) As Stream
as some sites have suggested.


Is it a multi=part problem?

0
Accepted
Roger
Top achievements
Rank 1
answered on 14 Jan 2014, 11:58 PM

OK

Done some more research:

WCF REST will not accept a post from Cordova 3.2.0 FileTransfer.upload.

Created an asmx web service and this accepts the post and downloads the file with filename, content type, etc, but cannot get parameters. All the posts about getting them from HttpContext.Current.Request.Params don't work.

Of course, POST does not accept querystrings so cannot send any identifying info with the file.

BUT

you can add headers to the FileTransfer.upload. config

var options  = new FileUploadOptions();

options.fileKey = "data";

options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);

options.mimeType = "image/jpeg";

options.chunkedMode = false;

options.headers = {Connection: "close", Data:["Test","param"]}

so when you look at params.GetValues("HTTP_DATA")(0)

where params = HttpContext.Current.Request.Params

you'll get a result of  "test,params" which you can do a split on the "," on to separate.

Either that or add individual headers.

Might not be the most elegant method but it works & you'll get the data as well as the file.

 

0
Accepted
Steve
Telerik team
answered on 15 Jan 2014, 04:32 PM
Hi Roger,

First - does your code work on iOS? If the problem is only with Android and if it works in Cordova 3.0 but does not in Cordova 3.2, and you've edited the Android config.xml then you've most likely hit the whitelisting breaking change in Cordova 3.2. For more information refer to our release notes here.

Regards,
Steve
Telerik
You've missed the Icenium Visual Studio Integration keynote? It has been recorded and posted here.
Looking for tips & tricks directly from the Icenium team? Check out our blog!
Share feedback and vote for features on our Feedback Portal.
0
Roger
Top achievements
Rank 1
answered on 16 Jan 2014, 10:16 PM
Hi Steve,
no nothing like that...I have an asp.net application. I added WCF to do the REST with the phone and that works fine. Now I'm getting into PhonGap and the filetransfer. This uses multi-part forms to transfer the file and WCF can't handle multi-part unless you change the binding:
<webHttpBinding>
       <binding name="WebConfiguration" 
                maxBufferSize="65536" 
                maxReceivedMessageSize="2000000000"
                transferMode="Streamed">
       </binding>
     </webHttpBinding>

of course if I do that all the other REST functions don't work. Without the streamed transfer mode the endpoint is not recognised and that's where I get the file-not-found error....I thought it was the file on the phone, but no.

So I used an asmx page for a web service to handle the filetransfer POST and that works fine...except that none of the parameters come through (tried params and form collection). I had to add them to the options as a header:

options.headers = {Connection: "close",
                   fileID:fileURI.substr(start,end-start),
                   id:'0A1A7BF8-8E7A-438E-80CC-22892DEBBCC3'
      }
and get them out the other end via:
HttpContext.Current.Request.Params.GetValues("HTTP_FILEID")(0)
HttpContext.Current.Request.Params.GetValues("HTTP_ID")(0)

of course you also get the file
HttpContext.Current.Request .Files(fileKey)

There's probably other ways to do this (ie add another bindable endpoint to the WCF that uses streaming) but I've spent enough time on this and just need to get the app in the store & this will get me the data I need.

So it's nothing to do with phonegap  as it works on both Android and Apple.

Be aware that if you add an asmx page to the solution after the WCF then the WebHTTPBinding will be affected and you'll lose the existing functionality of the WCF REST. I had to restore my solution from a precious endpoint and then copy an existing asmx page and turn it into the endpoint for the filetransfer post.

The original app is now working fine in all it's REST glory and I have the added functionality of multi-part POST.
I tried it both in Cordova 3.0 & 3.2 with same result so I'll stick with 3.2
Tags
Apache Cordova
Asked by
Roger
Top achievements
Rank 1
Answers by
Roger
Top achievements
Rank 1
Steve
Telerik team
Share this question
or