上传图片,并在同一时间发短信

我试图图像和一些文字同时发送到服务器。

我使用的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

和我有以下使用Web客户端用于发送图像:

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");

但我无法弄清楚如何在同一时间两者都做。

--------------解决方案-------------

经过大量的搜索,我发现了以下链接:

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

?>

保重,利

最好的办法很可能是添加一个标头信息,然后解析结果。

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

分类:C# 时间:2015-03-14 人气:0
本文关键词: .NET,vb.net
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 2.522 (s). 10 q(s)