public Stream GetDataFile(string locationId)
{
int cid;
DateTime tran = DateTime.Now.Date;
Check(string.IsNullOrWhiteSpace(locationId), "locationId field is missing.");
Check(!int.TryParse(locationId, out cid), "locationId must be numeric.");
DataFileRepository repository = new DataFileRepository();
var resp = WebOperationContext.Current.OutgoingResponse;
resp.ContentType = "application/octet-stream";
resp.Headers.Add("Content-Disposition", "attachment; filename=\"myfile.zip\"");
return ZipStream(repository.GetCSVStream(cid));
}
<summary>
</summary>
<param name="condition"></param>
<param name="exp"></param>
public void Check(bool condition, string exp)
{
if (condition)
{
AssertionException up = new AssertionException(exp);
throw up;
}
}
<summary>
</summary>
<param name="stream"></param>
<returns></returns>
public Stream ZipStream(Stream stream)
{
stream.Position = 0;
var ar = new ZipArchive();
ar.AddItem("myfile.csv", stream, true, FileAttributes.Normal);
var zip = new TempFileStream();
ar.Save(zip, false);
zip.Position = 0;
return zip;
}
interface declaration in API webservice is very simple:
[OperationContract]
[WebGet(UriTemplate = "menu/datafile/{locationId}")]
Stream GetDataFile(string locationId);