Jump to content
Sign in to follow this  
cjulio

Utilizar o WinHttpRequest para upload em VFP

Recommended Posts

cjulio

Boas pessoal.

Eu estou a tentar  usar o WinHttpRequest para fazer upload mas não estou a conseguir.

O código que utilizo no VFP é:

filecontent = FileToStr(ficheiro)

loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")    
loHTTP.Open("POST", "http://www.-------.com/webspace/upload.php", .F.)
loHTTP.SetRequestHeader("content-type", "text/plain")
loHTTP.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)")

varrequest	= 'attachment; filename="' + JUSTFNAME(ficheiro) + '"'

loHTTP.SetRequestHeader("content-disposition", varrequest)
loHTTP.Send(filecontent)

WAIT WINDOW TRANSFORM(loHTTP.status) + " - " + loHTTP.responseText + " - " + loHTTP.ResponseBody

 

O Codigo em PHP é:

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

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

 


o log do PHP esta a dar estes erros

[12-Dec-2018 12:28:02 UTC] PHP Notice:  Undefined index: file in /home/webspace/upload.php on line 3
[12-Dec-2018 12:28:02 UTC] PHP Notice:  Undefined index: file in /home/webspace/upload.php on line 5

Alguém pode ajudar?

Obrigado.

* Dupliquei o tópico porque não sei onde seria o mais indicado para fazer a questão.

https://www.portugal-a-programar.pt/forums/topic/77002-utilizar-o-winhttprequest-para-upload-em-vfp/

Share this post


Link to post
Share on other sites
cjulio

Mudei o codigo mas agora quando faço var_dump o ficheiro aparece como HTTP_RAW_POST_DATA

Código actual:

    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) 

var_dump no PHP

 

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

Content-Dis-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) {
    ["PATH"]=>
    string(28) "/usr/local/bin:/usr/bin:/bin"
    ["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"
    ["gzip-only-text/html"]=>
    string(1) "1"
    ["SERVER_SIGNATURE"]=>
    string(0) ""
    ["SERVER_SOFTWARE"]=>
    string(6) "Apache"
    ["SERVER_PROTOCOL"]=>
    string(8) "HTTP/1.1"
    ["REQUEST_METHOD"]=>
    string(4) "POST"
    ["QUERY_STRING"]=>
    string(0) ""
    ["REQUEST_TIME_FLOAT"]=>
    float(1544698207.5962)
    ["REQUEST_TIME"]=>
    int(1544698207)
  }
  ["GLOBALS"]=>
  *RECURSION*
} 

 

Edited by cjulio

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.