我工作的一個API類型的項目,它接受一個文件的字節數組web服務,怎麼辦httpPost使用C#
我已經寫了一個WebMethod (不完全是。我使用MVC創建像REST API)
public UploadFileImage(string employeeId, byte[] imageBytes, string imageName)
{
// saves the imagebyte as an image to a folder
}
Web服務將通過一個Web應用程序被消耗掉,或窗口,甚至iPhone或這種便攜式的東西。我正在通過簡單的httpPost使用Web應用程序測試我的Web服務。
string Post(Uri RequestUri, string Data)
{
try
{
HttpWebRequest request = HttpWebRequest.Create(RequestUri) as HttpWebRequest;
request.Method = "POST";
request.ContentType = IsXml.Checked ? "text/xml" : "application/x-www-form-urlencoded";
byte[] bytes = Encoding.ASCII.GetBytes(Data);
Stream os = null; // send the Post
request.ContentLength = bytes.Length; //Count bytes to send
os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
return streamReader.ReadToEnd();
}
catch (Exception ex)
{
return ex.Message;
}
}
此代碼工作正常埃維方法一樣,AddEmployee,DeleteEmployee等參數數據爲形式的 「ID = 123 &名= ABCDEFGH & DESIG =開發」,
如何調用任何其他函數是 Post(new Uri(「http:// localhost/addemployee」),「name = abcd & password = efgh」)
其中post是我寫的函數。
所有功能都很好。除了我不知道如何使用UploadFileImage來上傳圖片?
謝謝
您是不是要找使用Convert.ToBase64String(fileByteArray),其中fileByteArray是文件的字節數。 – eMan 2011-05-18 06:51:10
謝謝,多數民衆贊成工作,而張貼,發佈爲base64編碼的字符串,並在web服務,解碼它。 – eMan 2011-05-18 08:27:37
很高興幫助。也許你可以接受答案! – 2011-05-18 08:56:44