开发者

Cocoa Server Question

开发者 https://www.devze.com 2023-04-01 04:05 出处:网络
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? Thank

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号