×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

HTTP Post to upload file

HTTP Post to upload file

HTTP Post to upload file

(OP)
Hello everyone.

I am trying to use WinHttpRequest to upload a file but it is not working.

this is the code i have, it gives me a response of 200 but the file isn't uploaded.

The VFP Code

CODE --> VFP

filecontent = FileToStr(ficheiro)

	loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")    
	loHTTP.Open("POST", "http://site.com/webspace/upload.php", .F.)
	loHTTP.SetRequestHeader("content-type", "text/plain") 											&& or other mime types for other file types.

	varrequest	= 'attachment; filename="' + JUSTFNAME(ficheiro) + '"'
	loHTTP.SetRequestHeader("content-disposition", varrequest)

	loHTTP.Send(filecontent)
	WAIT WINDOW loHTTP.status 



The code in upload.php


CODE --> PHP

<?php
    $uploaddir = 'upload/';
    $uploadfile = $uploaddir . $_FILES['file']['name'];

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        echo "File successfully uploaded.\n";
    }
?> 



can anyone help please.
thanks.


Sorry for my English :)

RE: HTTP Post to upload file

Not very familiar with your situation, I personally find it better to trigger a download if the file is of any size at all...

That said, have you checked the permissions for you target folder?

Regards

Griff
Keep Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

RE: HTTP Post to upload file

(OP)
I have another app that is working just fine with the upload.

RE: HTTP Post to upload file

Quote:

I have another app that is working just fine with the upload.

Then you must ask yourself: What is different between the two apps? Is your VFP code different? Or the PHP? Are the files being uploaded to different servers? Are there any differences in their configuration?

If you can decide what's different, it will help you work out what is going wrong.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: HTTP Post to upload file

(OP)
from what i understand of the php error log i am not parsing this variable in vfp.

CODE --> PHP

$_FILES['file']['name'] 

is there a way for me to do this.

RE: HTTP Post to upload file

The code and headers you use are good for SENDING a file to a browser and telling it to present a donload/save as dialog. This is not good to upload files.
If you want to have a file received in $_FILES, it needs to come from a html form input type file tag or you need a new PHP way to read the sent http request.
Look into this as recieving coe:

CODE

<?php
$input = file_get_contents("php://input");
echo $input;
?> 

See what you receive that way.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: HTTP Post to upload file

(OP)
i changed the code a now i get the file as "HTTP_RAW_POST_DATA" when i do a var_dump in PHP

current code:

CODE --> VFP

loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")    
		loHTTP.Open("POST", "http://www.----------.com/webspace/getfile.php", .F.)
		xBOUNDARY	= "+++++"
		vbCrLf		= CHR(10) + CHR(13)

		filecontent = "--" + xBOUNDARY + vbCrLf
 		filecontent = filecontent + 'Content-Disposition: form-data; name="file"; filename="' + JUSTFNAME(ficheiro) + '"' + vbCrLf
 		filecontent = filecontent + "Content-type: text/plain" + vbCrLf + vbCrLf
 		filecontent = filecontent + FileToStr(ficheiro)
 		filecontent = filecontent + vbCrLf
 		filecontent = filecontent + "--" + xBOUNDARY + "--"
 		filecontent = filecontent + vbCrLf

		loHTTP.SetRequestHeader("CONTENT_TYPE", "multipart/form-data; boundary=" + xBOUNDARY + vbCrLf)
		loHTTP.SetRequestHeader("CONTENT_NAME", JUSTFNAME(ficheiro) + vbCrLf)

		loHTTP.Send(filecontent) 


CODE --> PHP

array(7) {
  ["HTTP_RAW_POST_DATA"]=>
  string(245) "--+++++

Content-Disposition: form-data; name="file"; filename="Oo2Teste.txt"

Content-type: text/plain


00000000000000|00000000000000| BASE|00|GERAL|00|00
1.1|1.1|Leiria - PD Azambuja - CMR1|00|GERAL|00|00
T|T|TESTE|00|GERAL|00|00


--+++++--

"
  ["_GET"]=>
  array(0) {
  }
  ["_POST"]=>
  array(0) {
  }
  ["_COOKIE"]=>
  array(0) {
  }
  ["_FILES"]=>
  array(0) {
  }
  ["_SERVER"]=>
  array(38) {
    ["TEMP"]=>
    string(4) "/tmp"
    ["TMP"]=>
    string(4) "/tmp"
    ["TMPDIR"]=>
    string(4) "/tmp"
    ["PWD"]=>
    string(1) "/"
    ["HTTP_ACCEPT"]=>
    string(3) "*/*"
    ["HTTP_CONNECTION"]=>
    string(10) "Keep-Alive"
    ["CONTENT_TYPE"]=>
    string(25) "text/plain; Charset=UTF-8"
    ["CONTENT_LENGTH"]=>
    string(5) "20341"
    ["HTTP_USER_AGENT"]=>
    string(57) "Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)"
    ["HTTP_CONTENT_TYPE"]=>
    string(35) "multipart/form-data; boundary=+++++"
    ["HTTP_CONTENT_NAME"]=>
    string(14) "Oo2Teste.txt"
  }
  ["GLOBALS"]=>
  *RECURSION*
} 

RE: HTTP Post to upload file

Well, yes, so the file is in the raw data, you have to pull it out from there, you don't get it into $_FILES, that would need a html form and input type file. And if you would like to emulate that, you need to lean more about the headers you need, the way you did that does not arrive as formdata. But you can extract the file from the raw input, it's not that hard, especially when you set your own boundary lines.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: HTTP Post to upload file

(OP)
I know i can get the file now but i wanted it to work without the var_dump.

RE: HTTP Post to upload file

>I know i can get the file now but i wanted it to work without the var_dump.
You have to do the correct emulation of a form submit.

Your question is an HTTP question, not a VFP question. Use something like fiddler and a working web form to see what POST headers and body you need.
One note: If you use the SetRequestheader method, you only provide header and value, not a CRLF.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: HTTP Post to upload file


try using this sample:

CODE -->

* 

#Define crlf Chr(13)+Chr(10)

boundary =  '--'+Strtran(Sys(2015),'_','')
srcFile = "x:\somefolder\upload\capture.png"
content = Filetostr(m.srcFile)
fileName = justfName(m.srcFile)
name = "file1"

cPayload = ;
    +m.boundary;
    +crlf;
  +textmerge('Content-Disposition: form-data; name="<<m.name>>"; filename="<<m.fileName>>"' );
    +crlf;
  +crlf;
  +m.content;
   +crlf;
  +m.boundary;
    +crlf

qPayload = Createbinary(m.cPayload)

* send

url="https://www.url.com"

With Createobject("MSXML2.ServerXMLHTTP") As msxml2.serverxmlhttp
       .open("POST",m.url,.F., m.usuario , m.password )
       .setRequestHeader("content-type", "multipart/form-data; boundary="+ Substr(m.boundary,3) )
      .send( m.qPayload)
Endwith 

Marco Plaza
@nfoxProject
https://www.github.com/nftools

RE: HTTP Post to upload file

(OP)
Thanks it is working.

Can you help me use the Eventhandler to monitor the upload?

With WinHttpRequest i am only able to monitor the download.

Thanks.

RE: HTTP Post to upload file

While we're at that topic of an event handler, I had C0000005 errors when using winhttp.winhttprequest.5.1, but I can't do a repro. Code that fails at one time works next day. It's important to say next day, a Foxpro restart does not help straighten things like any session that could be responsible for such problems.

I am currently working on an API that is actually intended to be consumed by a web application and even involves an interactive request causing a popup form which sending back its result via JS postMessage to a JS eventHandler listining for messages coming back. It needs IE11 to be able to use this mechanism in the webbrowser control, so I barely got that to work, but that's leading off topic.

I still did the normal non-interactive API requests with winhttp.winhttprequest.5.1 and asynchronously waiting for results with an eventhandler. I get C5 errors at the moment I send requests, even before the event handling plays a reole, because as you correctly stte, the iwinhttprequestevents interface you implement for that only has response events.

While the usage of webbrowser contorl and JS is a bit complicated, I think using JS Xmlhttprequest offers more individual handling and event monitoring, no matter if you use jQuery or native JS, you'll have an easy waay to declare JS functions for onload and many more events, also for respect of upload progress see https://developer.mozilla.org/en-US/docs/Web/API/X...

The JS world has advanced ways to act on http equests than you get from the COM classes usable directly in VFP, but a brodge from JS to VFP is quite easy, all you need is a global JS variable defined outside of all scrtipts and a setref function to set it, idea stolen from Rick Strahl: https://microsoft.public.fox.vfp.web.narkive.com/u...

The only downside with that is, it still depends on either IE automation or webbrowser control usage. You can keep these invisible, but I also had bad stability about the DocumentCompleteEvent and you can't use VFP BindEvents to bind to OLE properties, i.e. trying ti bind to winhttprequest.readyState you get PRoperty 'readyState' not found.

That's all the effects of working with legacy stuff, I guess.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: HTTP Post to upload file

Hay alguien que ando eso para COT de ARBA (Código de Operación Traslado o Transporte)? Conmigo no anda. sad

RE: HTTP Post to upload file

En inglés por favor

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close