Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTTP Post to upload file 1

Status
Not open for further replies.

Julio_MGM

Programmer
Dec 12, 2018
15
PT
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:
	filecontent = FileToStr(ficheiro)

	loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")    
	loHTTP.Open("POST", "[URL unfurl="true"]http://site.com/webspace/upload.php",[/URL] .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


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 :)
 
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 [Smile]ing

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.
 
I have another app that is working just fine with the upload.
 
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
 
from what i understand of the php error log i am not parsing this variable in vfp.

PHP:
$_FILES['file']['name']

is there a way for me to do this.
 
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
 
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:
		loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")    
		loHTTP.Open("POST", "[URL unfurl="true"]http://www.----------.com/webspace/getfile.php",[/URL] .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)


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*
}
 
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
 
I know i can get the file now but i wanted it to work without the var_dump.
 
>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
 
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="[URL unfurl="true"]https://www.url.com"[/URL]

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
 
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.
 
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
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:
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
 
Hay alguien que ando eso para COT de ARBA (Código de Operación Traslado o Transporte)? Conmigo no anda. [sad]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top