嘗試以輕量級的方式將字節數組傳送到ASP.NET 2.0應用程序,而不使用SOAP。我決定使用通用的HTTP處理程序(.ashx)將HTTP請求主體解釋爲Base64字符串,將其解碼爲字節數組並保存到磁盤。在ASP.NET 2.0應用程序中通過HTTP接收字節數組
<%@ WebHandler Language="C#" Class="PdfPrintService" %>
using System;
using System.Web;
public class PdfPrintService : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string body;
using (System.IO.StreamReader reader =
new System.IO.StreamReader(context.Request.InputStream))
{
body = reader.ReadToEnd();
}
byte[] bytes = System.Convert.FromBase64String(body);
String filePath = System.IO.Path.GetTempFileName() + ".pdf";
System.IO.File.WriteAllBytes(filePath, bytes);
// Print file.
XyzCompany.Printing.PrintUtility.PrintFile(filePath);
}
public bool IsReusable {
get {
return false;
}
}
}
客戶端應用(iOS應用在我的情況),將只需要編碼字節爲Base64並將其發佈到這個通用的處理程序(ASHX)的URL。
我想有一個更好,更正統的方法來做到這一點。任何想法表示讚賞!
首先爲什麼要打擾Base64? – leppie 2013-02-13 20:38:52
嗨Leppie。我建議使用Base64作爲在請求正文中傳輸PDF文件字節的一種方法。據我瞭解(我可能是錯的),如果你想通過HTTP傳輸二進制數據,你必須將它編碼爲一個字符串,並且base64似乎是流行的選擇(即.NET Web服務期望在SOAP中使用base64消息的字節數組參數)。也許更好的方式存在? – txdev 2013-02-14 06:15:30