我必須調用asmx webservice,它接受AttachmentData作爲參數。這有一個類型爲base64Binary的成員。如何在調用asmx webservice時發送base64Binary的數據
<s:complexType name="AttachmentData">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="FileName" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="UploadedUserName" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="Attachment" type="s:base64Binary" />
</s:sequence>
</s:complexType>
我送一個文件的內容爲附件成員如下:
//read the file contents
byte[] buffer = null;
try {
FileInfo attachment = new FileInfo(filepath);
using (FileStream stream = attachment.OpenRead()) {
if (stream.Length > 0) {
buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
}
}
}
catch {
buffer = null;
}
//create AttachmentData object
WebSrvc.AttachmentData att = new WebSrvc.AttachmentData();
att.FileName = fileName;
att.Attachment = buffer;
這是送base64Binary的正確方法是什麼?我是否需要將文件內容編碼爲base64或者是否即時完成?我正在嘗試通過使用上面的代碼來看看是否會不必要地膨脹web服務請求的大小。
你有這個代碼的問題嗎? – 2013-03-14 13:45:56
不,它工作正常,但我只是在考慮請求的大小。只要檢查一下,看看是否有更好的方法可以在請求中以較小的尺寸做同樣的事情。此外,我想了解如何將字節數組轉換爲base64Binary和字節數組和base64Binary之間的區別是什麼大小明智。 – 2013-03-14 13:49:56