VBSCRIPT上传文件到服务器

时间:2016-02-25 01:06:04

标签: php vbscript

我正在尝试制作一个脚本,将任何文件上传到简单的html / php上传表单。 我找不到任何不使用ASP的工作脚本。 这是我最接近的代码:(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

这给了我这个错误:

  

第9行   Char 1
  错误:对象时不允许操作   已关闭   代码800A0E78
  来源ADODB.Stream

PHP代码是:

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

请帮助!

2 个答案:

答案 0 :(得分:3)

基本上有4个错误需要修复:

  • set
  • 移除line 9
  • objStream.Read更改为objStream.ReadText
  • line 9移至objStream.LoadFromFile
  • 之后
  • set
  • 移除line 14

完整代码:

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

答案 1 :(得分:0)

这对我不起作用,但我发现了这个: https://github.com/ArancioGrigio/vbs-php-ImageUpload

它通过将图像转换为 base64 并将文本以 2000 个字符的数据包通过 php GET 参数发送到服务器来将图像传输到 php 服务器。最后,php 服务器将文本转换回图像。 我使用 vbs 作为客户端和 php 作为服务器。 这对于低流量和低重量图像很有用,否则可能需要几分钟时间并且服务器无法再响应。 GET 参数限制为 2048 个字符