上传图片并同时发送文字

时间:2012-03-05 03:24:13

标签: c# .net vb.net

我正在尝试同时向服务器发送图像和一些文本。

我正在使用WebRequest,如下所示发送文本:

Dim ba As Byte() = Encoding.UTF8.GetBytes(query)

Dim wr As WebRequest = WebRequest.Create(Me.server_url)
wr.Method = "POST"
wr.ContentType = "application/x-www-form-urlencoded"
wr.ContentLength = ba.Length

我有以下使用WebClient发送图像:

System.Net.WebClient Client = new System.Net.WebClient();

Client.Headers.Add("Content-Type", "binary/octet-stream");

byte[] result = Client.UploadFile(Properties.Settings.Default.script_url, 
"POST", "desktop.png");

但我无法弄清楚如何同时做两件事。

2 个答案:

答案 0 :(得分:2)

经过大量搜索后,我找到了以下链接:

http://www.paraesthesia.com/archive/2009/12/16/posting-multipartform-data-using-.net-webrequest.aspx

这是针对C#的,所以这是我为VB编写的概念证明:

Dim boundry As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")
    Dim request As WebRequest = WebRequest.Create(Globals.script_path + "uploader.php")
    request.Method = "POST"
    request.ContentType = "multipart/form-data; boundary=" + boundry

    Dim requestStream As Stream = request.GetRequestStream()

    '' send text data
    Dim data As Hashtable = New Hashtable()
    data.Add("text_input", "Hello World")

    For Each key As String In data.Keys
        Dim item As String = "--" + boundry + vbCrLf + "Content-Disposition: form-data; name=""" & key & """" + vbCrLf + vbCrLf + data.Item(key) + vbCrLf
        Dim itemBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(item)
        requestStream.Write(itemBytes, 0, itemBytes.Length)

    Next

    '' send image data
    Dim file_header = "--" + boundry + vbCrLf + "Content-Disposition: form-data; name=""file_1"";filename=""file.png""" + vbCrLf + "Content-Type: image/png" + vbCrLf + vbCrLf
    Dim file_header_bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(file_header)
    requestStream.Write(file_header_bytes, 0, file_header_bytes.Length)
    Dim ms As MemoryStream = New MemoryStream()
    timecard.screen_shot.Save(ms, ImageFormat.Png)
    Dim file_bytes() As Byte = ms.GetBuffer()
    ms.Close()
    requestStream.Write(file_bytes, 0, file_bytes.Length)
    Dim file_footer_bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(vbCrLf)
    requestStream.Write(file_footer_bytes, 0, file_footer_bytes.Length)


    '' send
    Dim endBytes() As Byte = System.Text.Encoding.UTF8.GetBytes("--" + boundry + "--")
    requestStream.Write(endBytes, 0, endBytes.Length)
    requestStream.Close()



    Dim response As WebResponse = request.GetResponse()
    Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
    Debug.WriteLine(reader.ReadToEnd())

这当然是为了满足我的个人需求,但我认为它很好地描述了需要做的事情。对于图像,有一个文件名。在这种情况下,它不是一个实际的文件。但是接收文件的PHP需要数据。

以下是我用来测试它的PHP代码:

<?php
if (isset($_POST['text_input'])){
echo $_POST['text_input'];
    $target_path = "screenshots/";

    $target_path = $target_path . basename( $_FILES['file_1']['name']); 

    if(move_uploaded_file($_FILES['file_1']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['file_1']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}else{
    echo "no dta";
}

&GT;

小心, 利

答案 1 :(得分:0)

最好的方法可能是添加包含信息的Header,然后解析结果。

Client.Headers.Add("MyText", "MyString");