1

What is the difference when returning files between

public Stream getFile(string filename){
    Stream s = _getFileStream(filename);
    Response.AddHeader( "Content-Disposition", "attachment;filename=" + filename+ ";" );
    return s;
}

and

public void getFile(string filename){
    byte[] b = _getFileBytes(filename);
    Response.AddHeader( "Content-Disposition", "attachment;filename=" + filename+ ";"  );
    Response.BinaryWrite(b)
}

and the method provided in the question here.

The problem I'm encountering is that sometimes parts of the image a user has uploaded appear scrambled. What's also odd is I cannot reproduce the problem locally - only when the application is on the www.

My thinking is that perhaps since my resources are local I wouldn't see the problem when streaming data and that the way I am streaming content back to the client isn't correct. So which is the "correct" (or recommended) way of returning the file?

Community
  • 1
  • 1
spots
  • 2,483
  • 5
  • 23
  • 38
  • First function does not write and data. 2nd returns undeclared variable `s` in a `void` function. – Magnus May 08 '13 at 15:02
  • Whoops, I didn't proof-read. Both functions work for me. I just want to know if one is more better than the other and why. – spots May 08 '13 at 15:06
  • If you're mixing WCF (I think, based on the tags in the question) with plain ASP.NET - you shouldn't do that. Either go full ASP.NET (and forget about `[ServiceContract]` and friends) where you can use the Response object, or go full WCF (where you return `Stream` but use the `WebOperationContext.Current.OutgoingResponse.Headers` to add the returning header. – carlosfigueira May 08 '13 at 17:49
  • I am accessing the Response by HttpContext.Current.Response. I want to go the WCF route and avoid the ASP.net stuff. – spots May 08 '13 at 18:28

1 Answers1

0

I resolved my issue with two changes.

  1. I took what carlosfiguera said and changed HttpContext.Current.Response.AddHeaders(...) to WebOperationContext.Current.OutgoingResponse.Add(...)
  2. In my WebConfig I located the binding tag my service had it's bindingCOnfiguration set to and changed transferMode to "Streamed"

These two changes got my images showing up correctly.

spots
  • 2,483
  • 5
  • 23
  • 38