简体   繁体   中英

VBSCRIPT Upload File to Server

I am trying to make a script to upload any file to a simple html/php upload form. I cannot find any working scripts that don't use ASP. This is the closest code I have: (VBS)

Dim strURL
Dim HTTP
Dim dataFile
Dim dataRequest
Dim objStream
strURL = "http://10.0.0.50/~/v_upload/up.php"
Set HTTP = CreateObject("Microsoft.XMLHTTP")
Set objStream = CreateObject("ADODB.Stream")
Set dataFile = objStream.Read
objStream.Type = 2
objStream.Open
objStream.LoadFromFile "http.txt"

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

This give me this error:

Line 9
Char 1
Error: Operation is not allowed when the object is closed
Code 800A0E78
Source ADODB.Stream

The PHP code is:

<?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.";
}
?>

Please Help!

There are basically 4 errors that need to be repaired:

  • Remove set from line 9
  • Change objStream.Read to objStream.ReadText
  • Move line 9 to after objStream.LoadFromFile
  • Remove set from line 14

The Full Code:

Dim strURL
Dim HTTP
Dim dataFile
Dim dataRequest
Dim objStream
strURL = "http://10.0.0.50/~/v_upload/up.php"
Set HTTP = CreateObject("Microsoft.XMLHTTP")
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = 2
objStream.Open
objStream.LoadFromFile "http.txt"
dataFile = objStream.ReadText

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

This did not work for me, but I found this: https://github.com/ArancioGrigio/vbs-php-ImageUpload

It transfers image to a php server by converting them to base64 and sending the text to server in packets of 2000 character through php GET parameters. At the end the php server converts text back to image. I used vbs for client and php for server. This is good for low traffic and low weight images, otherwise it could take some minutes and server could not respond anymore. GET parameters limit is 2048 characters

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