1

I have a web service that needs to return a large text file for an AJAX call on the client. For starters, I have a valid path to the text file:

var fileName = <file on server>

I know the file name is valid, because I can open it on the server as a FileStream.

I have tried some of the different approaches recommended in ServiceStack and returning a stream, but I can't seem to get it to work.

What should I return? My best guess was:

var stream = File.Open(fileName, ...);
return HttpResult(stream, "text/plain"){ AllowPartialResponse = false };

But that doesn't work; the response is a simple JSON object. Using FileInfo with the asAttachment option didn't work either; it just returned a bunch of file information.

The goal is for the JavaScript client to be able to receive the content as a string:

api.getFile({...}).then( function (result) {
    // result has the file contents
}); 

What is the correct way to do this?

Update:

I ended up using this stream approach to get it working:

using( var fs = File.OpenRead( fileName ) )
{
    fs.CopyTo( Response.OutputStream );
}

I don't fully understand why some of the other approaches didn't work, but they appear to be related to 1) FileInfo and how it behaves on the server, 2) file permissions in my development environment, and 3) "partial content" problems and exceptions. File.Exists() returns false, but not in debug discusses one of the problems that was throwing me off.

Community
  • 1
  • 1
Seth
  • 6,514
  • 5
  • 49
  • 58

1 Answers1

4

I've just committed a Service that returns a text file and the HttpResult API's work as expected:

[Route("/textfile-test")]
public class TextFileTest
{
    public bool AsAttachment { get; set; }
}

public class MyServices : Service
{
    public object Any(TextFileTest request)
    {
        return new HttpResult(new FileInfo("~/textfile.txt".MapHostAbsolutePath()), 
            asAttachment:request.AsAttachment);
    }
}

Which you can test out with or without the asAttachment option:

You can also access the text file directly (i.e. without a Service):

In addition, the different ways to return an Image response should also apply to text files as well.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thank you for the quick response. Here is the error I'm getting, regardless of the size of the file: https://gist.github.com/sethlivingston/fc7236c59d4f11a4124e. Any ideas? – Seth Jan 15 '15 at 15:11
  • @Seth Can you access the file directly via the url? Can I get you to update your question to include the raw HTTP Request and Response headers that's returning this error? You can get this from Chrome's WebInspector or Fiddler. – mythz Jan 15 '15 at 16:29
  • Updated the gist: https://gist.github.com/sethlivingston/fc7236c59d4f11a4124e. Let me know if you need more. – Seth Jan 15 '15 at 16:45
  • ...and yes, I can access the file directly as a URL. – Seth Jan 15 '15 at 16:52
  • @Seth does removing the `.json` extension fix the issue? – mythz Jan 15 '15 at 17:10
  • Removing .json produces a page with "Snapshot of DocDtoReplication generated by ServiceStack on 1/15/2015 5:16:07 PM". The contents of the file aren't there. – Seth Jan 15 '15 at 17:17
  • ...I get the same error on IIS Express as I do on IIS 7 local, FWIW. – Seth Jan 15 '15 at 17:39
  • @Seth I'm having a real hard time trying to repro this, I've tried using your HTTP Request headers and have also created an [JS Bin demo](http://jsbin.com/teruxonuco/1/edit?html,output) to test the result from an ajax request. Are you sure the file exists, i.e. can you add `if (!FileInfo.Exists) throw HttpError.NotFound("Not Found");` – mythz Jan 15 '15 at 17:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68905/discussion-between-seth-and-mythz). – Seth Jan 15 '15 at 17:55
  • @Seth can't chat atm, I'm in the middle of preparing for a release right now. Could I get you to create a small stand-alone repro (i.e. GitHub?) that I can run and repro on my side? – mythz Jan 15 '15 at 18:06