简体   繁体   中英

Uploading Text File via HTTP using vbscript

I want to upload a text file from local drive to web server by HTTP using vbscript. How to do this? I need to upload a .txt file into server .

Vbscript code I used:

strUserID = "******" 
strPassword = "******" 

  strURL =   "http://testpias.site50.net/http/up.php"

Set HTTP = WScript.CreateObject("Microsoft.XMLHTTP")
Set fso = CreateObject("Scripting.FileSystemObject")



Set objStream = CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile "G:\Http\http.txt"




HTTP.open "POST", strURL & "http.txt", False , strUserID , strPassword
WScript.Echo "Now uploading file " & "G:\Http\http.txt"


HTTP.send objStream.Read


 WScript.Echo "Uploading complete for file " & fso.GetFileName(File)




WScript.Echo "All files uploaded."

Set HTTP = Nothing

PHP code I used to receive the file :

<?php

        $file_path = "http/";

        $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
        if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
            echo "success";
        } else{
            echo "fail";
        }
     ?>

But I got this error while I ran the vbscript , " Access is denied " .

For your VBScript, I would suggest the following (not using PUT ):

Dim strURL, HTTP, dataFile, dataRequest
Dim objStream As Object
strURL = "http://testpias.site50.net/http/up.php"
Set HTTP = CreateObject("Microsoft.XMLHTTP")
Set objStream = CreateObject("ADODB.Stream")

objStream.Type = 2
objStream.Open
objStream.LoadFromFile "G:\Http\http.txt"
set dataFile = objStream.Read
set dataRequest = "dataFile=" & dataFile

HTTP.open "POST", strURL, False
HTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
HTTP.setRequestHeader "Content-Length", Len(dataRequest)

WScript.Echo "Now uploading file G:\Http\http.txt"

HTTP.send dataRequest
WScript.Echo HTTP.responseText

Set HTTP = Nothing

Then in your PHP that will accept the file, you have to do some work too. Making sure that your handler is looking for the right index. Also we want to return some response so we know what happened.

<?php
if (!isset($_FILES['dataFile']['error']) || is_array($_FILES['dataFile']['error'])) {
    switch ($_FILES['dataFile']['error']) {
        case UPLOAD_ERR_OK:
            break;
        case UPLOAD_ERR_NO_FILE:
            echo 'Unable to Upload. No file sent.';
        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            echo 'Unable to Upload. Exceeded file size limit.';
        default:
            echo 'Unable to Upload. Unknown errors.';
    }
    die();
}
$file_path = "http/";
$file_path = $file_path . basename( $_FILES['dataFile']['name']);
if(move_uploaded_file($_FILES['dataFile']['tmp_name'], $file_path)) {
    echo "File {$_FILE['dataFile']['name']} uploaded success";
} else{
    echo "Unable to upload. Unable to move uploaded file.";
}
?>

Sorry I do not have the ability to test this. Hopefully this will get you in the right path.

Reference: http://www.motobit.com/tips/detpg_read-write-binary-files/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM