In .net web services is there a way where the file name can be sent in the SOAP request and the service returns the File itself as attachment in the SOAP response. I was thinking if the file can only be returned as a filestream object or some sort of .net I/O related object but not as an attachment. I was asked to check if this can be done? Any thoughts or comments?
+3
A:
What do you mean by attachment
? A SOAP response represents XML encoded data which is sent to the client. Binary structures such as byte[]
will be base64 encoded and sent in the response body. So the following method should do the job:
[WebMethod]
public byte[] GetFile(string filename)
{
// Add security checks, etc... as such method is begging for hacks
return File.ReadAllBytes(filename);
}
Also you should probably consider using WCF because ASP.NET web services are considered now as deprecated technology. There are also protocols over HTTP such as MTOM that support optimized binary data transmission over the wire.
Darin Dimitrov
2010-08-11 16:23:08
+1, Keep in mind, Base64 encoded string of a file could be **HUGE**.
Marc
2010-08-11 16:30:59
Yeap, that's why I mentioned `MTOM`.
Darin Dimitrov
2010-08-11 16:32:56