在 Visual Basic 2010 Express 上上载文件并接收响应



我知道如何将文件上传到PHP脚本以将文件存储到Web服务器,PHP脚本会生成唯一的文件名。然后我想将来自 PHP 脚本的响应与 VB 一起使用,有点像阅读响应。我的代码是:

Visual Basic:

Sub uploadfile()
    For Each fPath In FilePath
        My.Computer.Network.UploadFile(fPath, "http://mydomain.com/upload.php")
        MsgBox(fPath)
    Next
End Sub

.PHP:

<?php
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ($_FILES["file"]["error"] > 0){
    echo "Error";
}else{
    $newfile = uniqid("image_").".".$extension;
    move_uploaded_file($_FILES["file"]["tmp_name"], "Images/" . $newfile);
}
?>

我自己找到了解决方案:

For Each fPath In FilePath
    Dim client As New WebClient()
    Dim responseBinary As Byte()
    responseBinary = client.UploadFile("http://example.com/upload.php", fPath)
    Dim response As String
    response = Encoding.UTF8.GetString(responseBinary)
    MsgBox(response)
Next

研究人员您必须使用

For Each fPath In FilePath
    Using client As New WebClient()
        Dim responseBinary As Byte()
        responseBinary = client.UploadFile("http://example.com/upload.php", fPath)
        Dim response As String
        response = Encoding.UTF8.GetString(responseBinary)
        MsgBox(response)
    End Using
Next

最新更新