使用vbscript通过HTTP上载文本文件

时间:2015-10-18 18:49:39

标签: php html vbscript webserver

我想使用vbscript通过HTTP将文本文件从本地驱动器上传到Web服务器。这该怎么做?我需要将.txt文件上传到服务器。

我使用的Vbscript代码:

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代码:

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

但是当我运行vbscript时出现了这个错误,&#34;访问被拒绝&#34; 。

1 个答案:

答案 0 :(得分:0)

对于您的VBScript,我建议如下(不使用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

然后在你接受该文件的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.";
}
?>

抱歉,我没有能力测试这个。希望这会让你走上正确的道路。

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