2010-02-20 129 views
2

我試圖使用HTTP Post Multipart將圖像發送到服務器。其他一切都很好,我有所有的限制和東西。通過POST Multipart發送圖像(HTTPRequest)

但是我必須先對圖像做些什麼?我必須將其轉換爲二進制文件嗎?這是來自標題的標題數據(使用Fiddler)。這是我需要上傳的內容:

-----------------------------7daea2aa40c80 
Content-Disposition: form-data; name="pict"; filename="pic.jpeg" 
Content-Type: image/pjpeg 

<Binary here ... or at least I think it is> .. 
�����JFIF���������C� (lots more of this I removed) 

有什麼建議嗎?

回答

2

您可以使用File.ReadAllBytes函數將文件讀入一個byte []。從那裏你可以使用StreamWriter將字節輸出到你的響應中。沒有需要轉換。

+0

真棒東西閱讀多瞭解一下!花費數小時尋找與此相關的信息! – 2010-02-20 23:33:38

0

理想情況下,您希望將Content-Transfering-Encoding設置爲base64。然後你只需將File.ReadAllBytes文件放入一個字節數組中,然後使用Convert.ToBase64String方法將其轉換爲base64。

您可以在維基百科的文章約MIME

string data = 
     @"----------------------------7daea2aa40c80\n"; 
     + @"Content-Disposition: form-data; name="pict"; filename="{0}"\n" 
     + @"Content-Type: image/pjpeg\n"; 
     + @"\n{1}"; 

string filename = "pict.jpg"; 
string bytes = Convert.ToBase64String(File.ReadAllBytes(filename)); 
string request = string.Format(data, filename, bytes);