Android Question HttpJob.PostFile problem

nibbo

Active Member
Licensed User
Longtime User
Hi All

Having a spot of bother with HttpJob.PostFile; code below:
B4X:
Dim Job As HttpJob
Job.Initialize("CardImage", Me)
Job.PostFile(https://xxx.com/savefile.aspx?File=fred.jpg, File.DirRootExternal & "/" , "fred.jpg")
However this gets rejected by the server with a weird error:
Exception message: A potentially dangerous Request.Form value was detected from the client (="...�3O����<S����Ni1�Ɨ��,�...").
The event log shows Event Id 1309 code 3003; everything I have googled says I should remove the validation in the web page which I have tried but still get the same error.
This is probably a IIS issue but though I would try here first to see if anyone else has had this or may know of a fix.

Thanks in advance
 

nibbo

Active Member
Licensed User
Longtime User
HttpJob.PostFile creates a POST request with the file content as the message payload.
This will only work with a server that expects such requests.

Thanks for the reply Erel.
Got to the bottom of reason for the rejection in the end and it was an IIS config issue as I had originally thought.
The website is .NET 4 so as well as having
B4X:
validateRequest="false"
in the page header a line must also be added to the config file.
B4X:
<httpRuntime requestValidationMode="2.0" />

However; does anyone know what the asp should look like to retrieve and save the file on the server side?
Request.Files is empty.

Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Request.Files is empty
i suggest using okhttputils2´s multipartpost method

B4X:
    Dim Upload As HttpJob   
     Upload.Initialize("FileUpload",Me)
    Upload.Tag = "FileUpload"
   
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "file"
    fd.Dir = File.DirAssets
    fd.FileName = "se.jpg"
    fd.ContentType = "image/jpeg"
   
    Dim flist As List
    flist.Initialize
    flist.Add(fd)
   
    Dim NameValues As Map
    NameValues.Initialize
    NameValues.Put("uploadType","multipart")
    NameValues.Put("convert",False)
    NameValues.Put("ocr",True)
    NameValues.Put("pinned",False)
    NameValues.Put("ocrLanguage","de")
    NameValues.Put("timedTextLanguage","de")
    NameValues.Put("timedTextTrackName","de")
   
    Upload.PostMultipart(DriveUploadLink,NameValues,flist)
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Thanks DonManfred; I will try this if I have to but I would rather keep it simple and use the postFile method.
I have about 40 http jobs in the project and having more than one http library has caused problems in the past.
postFile is definitely posting the data and I can see that the data server side (using Request.SaveAs) but I am not sure how to retrieve it in the aspx code behind.
I have tried Request.InputStream and Request.Files but neither contain any data.
Anyone...
Thanks
 
Upvote 0

nibbo

Active Member
Licensed User
Longtime User
Finally worked it out so posting code here in case anyone else needs it.
The data is held in the Request.InputStream so...
B4X:
Dim byteArray(Request.InputStream.Length) As Byte
Request.InputStream.Read(byteArray, 0, Request.InputStream.Length)
Dim newFile As String = System.AppDomain.CurrentDomain.BaseDirectory & "UploadedImages\fred.jpg"
Dim fs As FileStream = New FileStream(newFile, FileMode.Create)
fs.Write(byteArray, 0, byteArray.Length)
fs.Close()

Thanks to all for the replies.
 
Upvote 0
Top