6

I want to make the following webpage using CocoaHTTPServer: there should be a link to download a file, but the source file must be NSData object in memory.

As far as I see in samples, there is an easy way to link some file on iPhone to the hyperlink. Is it possible to "link" NSData?

Would be very thankful for examples.

brigadir
  • 6,874
  • 6
  • 46
  • 81

1 Answers1

7

All you need to do is to return HTTPDataResponse in your HTTPConnection subclass.

If you want an example have a look at the CocoaHTTPServer sample called DynamicServer and replace - httpResponseForMethod: URI: in MyHTTPConnection with something similar to the following:

- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
    // Before returning you can analyze the passed path argument and select the correct data object to return...
    return [[HTTPDataResponse alloc] initWithData:placeYourDataInstanceHere];
}
miho
  • 11,765
  • 7
  • 42
  • 85
  • Hah! So simple, how I missed it... Thanks! – brigadir Oct 18 '12 at 16:59
  • How do you analyze the request data if it's a `POST` instead of a `GET`? For example, if `JSON` data is sent with the request. – zakdances Aug 25 '13 at 08:12
  • I'm not sure what you are interested to know. The request method (`GET`, `POST`, `PUT`, `DELETE`, ...) is send as the first argument `(NSString *)method` to the method. – miho Aug 30 '13 at 08:07