Cocoa Server Question
I'm trying to host multiple files on a server on iOS devices, and right now I'm able to show a simple text file. How would I change this code to display a HTML file in the documents directory? Thanks!
- (void)startResponse
{
NSData *fileData =
[NSData dataWithContentsOfFile:[AppTextFileResponse pathForFile]];
CFHTTPMessageRef response =
CFHTTPMessageCreateResponse(
kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1);
CFHTTPMessageSetHeaderFieldValue(
response, (CFStringRef)@"Content-Type", (CFStringRef)@"text/plain");
CFHTTPMessageSetHeaderFieldValue(
response, (CFStringRef)@"Connection", (CFStringRef)@"close");
CFHTTPMessageSetHeaderFieldValue(
response,
(CFStringRef)@"Content-Length",
(CFStringRef)[NSString stringWithFormat:@"%ld", [fileData length]]);
CFDataRef headerData = CFHTTPMessageCopySerializedMessage(response);
@try
{
[fileHandle writeData:(NSData *)headerData];
[fileHandle writeData:fileData];
}
@catch (NSException *exception)
{
// Ignore the exception, it normally just means the client
// closed the connection from the other end.
开发者_开发技巧 }
@finally
{
CFRelease(headerData);
[server closeHandler:self];
}
}
Change the Content-type from text/plain to text/html and make sure pathForFile is pointing to an HTML file.
精彩评论