2012-12-17 79 views
3

我想上傳與HttpWebRequest的&一切正常,直到我說要上傳的圖片多部分的形式,主要是我想作爲一個瀏覽器使得它看起來像這樣做完全一樣的要求:上傳圖片MultiPart表單HTTPWebRequest - 將圖像數據轉換爲字符串?

-----------------------------41184676334 
Content-Disposition: form-data; name="file"; filename="guitar tape.jpg" 
Content-Type: image/jpeg 

IMAGEDATAHERE 
-----------------------------41184676334 
Content-Disposition: form-data; name="save" 

save 
-----------------------------41184676334-- 

我迷失在如何格式化/讀取圖像將其設置成我下面提出請求:

  Dim boundary As String = "-----------------------------" & DateTime.Now.Ticks.ToString("x") 
     Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://www.mysite.com/upload.php"), HttpWebRequest) 
     req.Method = "POST" 
     req.ContentType = "multipart/form-data; boundary=" & "---------------------------" & DateTime.Now.Ticks.ToString("x") 
     req.KeepAlive = False 
     Dim builder As New StringBuilder() 
     builder.Append(boundary & vbCrLf & "Content-Disposition: form-data; name=""variable1""" & vbCrLf & vbCrLf & "1" & vbCrLf) 
     builder.Append(boundary & vbCrLf & "Content-Disposition: form-data; name=""file""; filename=""" & FileName & """" & vbCrLf) 
     builder.Append("Content-Type: application/octet-stream") 
     builder.Append(vbCrLf & vbCrLf) 
     ' Add Photo Here 
     If UpdateImage = True Then 
      ' Load Image 
      Dim ImageData As System.Drawing.Image 
      Dim fs As New System.IO.FileStream(ImagePath, System.IO.FileMode.Open) 
      ImageData = Image.FromStream(fs) 
      fs.Close() 
      ' Add Image To Header 
      builder.Append(ImageData) 
      builder.Append(vbCrLf) 
     Else 
      builder.Append(vbCrLf) 
     End If 
     builder.Append(boundary & vbCrLf & "Content-Disposition: form-data; name=""save""" & vbCrLf & vbCrLf & "save") 
     ' Footer Bytes 
     Dim close As Byte() = Encoding.UTF8.GetBytes("--") 
     Dim postHeader As String = builder.ToString() 
     Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(postHeader) 
     Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(vbCrLf & boundary & "--" & vbCrLf) 
     Dim length As Long = postHeaderBytes.Length + boundaryBytes.Length 
     req.ContentLength = length 
     Dim requestStream As Stream = req.GetRequestStream() 
     Dim fulllength As Integer = postHeaderBytes.Length + boundaryBytes.Length 
     ' Write out our post header 
     requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length) 
     ' Write out the trailing boundary 
     requestStream.Write(boundaryBytes, 0, boundaryBytes.Length) 
     Dim responce As WebResponse 
     responce = req.GetResponse() 
     requestStream.Close() 
     Dim s As Stream = responce.GetResponseStream() 
     Dim sr As New StreamReader(s) 
     Dim Content As String = sr.ReadToEnd() 

目前,它僅僅是張貼「System.Drawing.Bitmap」作爲圖像數據但我不知道如何獲得像這樣的圖像相同的原始數據:

J©õݨe‚Lnž¿Ëã/ǧúÐ5ý¼C÷Cý>ß’t;fm—=Äw:�/E±ËÙÏ$á@%Pc>× Šgw.²Ab「:ÅÓù:ϯÌh6à€Z§Ó‚g£®hÚD6¨Ø^Ú2ô`ä¨L�YÆÄÅCX#I「ÈÌãj¦L˜•’|¥�Eb¡ëQ–¤Ú, 3\UzL  öÔoj4�•±’u«c¼#„oÕ`îF>·o—ŠûÅ«ÎÑ™¶Ç˜ýº*i°œÈVŒ�Qû」Ñ[.�ÔmçE•ì¦eNCh�Ù 
é§�É$m¿ôš"»ÌNæ(VÌmp›F¹XÈ88™ªüµ…d•XµÔÜ#�ˆŠv‘º‚F‚§Yûb 

任何關於如何做到這一點的想法或是否需要改變我的方法?

回答

1
builder.Append(ImageData) 

是不正確的。您需要將圖像作爲字節讀取,然後將字節[]添加到多部分文章中。

詳情請參閱在Using HttpWebRequest to POST data/upload image using multipart/form-data

,並確保使用HTTP嗅探器(即小提琴手),看看它是實際發送。

第一負載圖像轉換成字節數組,然後將其轉換爲Base64:

imgBase64 = Convert.ToBase64String(my_image_byte_array) 
+0

感謝您的答覆,我以前試過這種與它類似的職位內容: /9J/4AAQSkZJRgABAQEAYABgAAD/2wBDAAICAgICAQICAgIDAgIDAwYEAwMDAwcFBQQGCAc的(Base64) 什麼我希望做的是發佈實際的圖像數據,比如上例中的數據。 – Chris

+0

如果您在提琴手中看到base64格式,那麼您必須將其作爲base64格式發佈。這是形象,只是以不同的格式。在服務器上,它將解碼base64格式以保存圖像。如果您發佈原始數據,則會失敗。 – urlreader

+0

目前的服務器響應是無效的圖像,雖然發佈的數據是100%好,但實際圖像數據是不同的格式。 – Chris

0

我的 「打開文件對話框」 名稱爲 「文件」。當點擊一個「按鈕」時,它會顯示一個彈出對話框,用於選擇jpg,jpeg,png圖像文件,還可以添加bmp或任何圖像文件類型。然後選擇一個圖像文件後,它將顯示在「PictureBox」上,我將其命名爲「picboxProfileID」。

在Visual Basic中:

file.Filter = "image file (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png | all files (*.*) | *.*" 
    If (file.ShowDialog <> Windows.Forms.DialogResult.Cancel) Then 
     picboxProfileID.Image = Image.FromFile(file.FileName) 
     picboxProfileID.ImageLocation = file.FileName 
     txtName.Text = file.FileName 
     My.Computer.Network.UploadFile(file.FileName, "http://localhost/VTVTS/uploadImageSample.php") 
    Else 
     picboxProfileID.Image = Nothing 
    End If 

上傳的文件的目的地將在創建父文件夾的PHP文件上傳「圖像/」。

在PHP:

<?php 
$response = array(); 
$image = $_FILES['file']; 

$imageName = $image['name']; 
$imageTmpName = $image['tmp_name']; 
$imageSize = $image['size']; 
$imageError = $image['error']; 
$imageType = $image['type']; 

$fileExt = explode('.', $imageName); 
$fileActualExt = strtolower(end($fileExt)); 

$allowed = array('jpg', 'jpeg', 'png'); 
if(in_array($fileActualExt, $allowed)){ 
    $fileNameNew = uniqid('', true).".".$fileActualExt; 
    $fileDestination = 'images/'.$fileNameNew; 
    $newDestination = move_uploaded_file($imageTmpName, $fileDestination); 
    $response['error'] = false; 
    $response['message'] = "Upload Image Successful"; 
}else{ 
    $response['error'] = true; 
    $response['message'] = "You cannot upload files of this type!"; 
} 
echo json_encode($response); 

編碼快樂!

相關問題