Understanding the file upload sample - Form action linked to another html form

48 views
Skip to first unread message

James25

unread,
Apr 6, 2015, 6:16:57 PM4/6/15
to cocoaht...@googlegroups.com
I am currently trying to understand the Cocoa Http Server regarding file transfer.Currently the form that is displayed when I connect to the http server via my browser is this. I select a file and it uploads fine. I am a bit confused as to what is called to handle the upload process.This is the form that is called. 

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">
    </head>
    <body>
    <form action="upload.html" method="post" enctype="multipart/form-data" accept-charset="utf-8">
    <input type="file" name="upload1"><br/>
                    <input type="file" name="upload2"><br/>
    <input type="submit" value="Submit">
    </form>
   
    
    </body>
    </html>

I am confused about the form `action` part. From what I understand is that it calls `upload.html` This is the code for `upload.html`

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">
    </head>
    <body>
    %MyFiles%
    </body>
    </html> 

My question is what is `%MyFiles%` ? What happens when a form posts to another html file  ?

 After doing a search for MyFiles in the entire project. I came up with this (this is the only place its being used). For convenience i have mentioned the objective C code below.

 **My main question is what happens when a html form action posts to another html form ? In my case what is the other html form doing ?**

    - (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
    {
    HTTPLogTrace();
   
    if ([method isEqualToString:@"POST"] && [path isEqualToString:@"/upload.html"])
    {
    
    // this method will generate response with links to uploaded file
    NSMutableString* filesStr = [[NSMutableString alloc] init];
    
    for( NSString* filePath in uploadedFiles ) {
    //generate links
    [filesStr appendFormat:@"<a href=\"%@\"> %@ </a><br/>",filePath, [filePath lastPathComponent]];
    }
    NSString* templatePath = [[config documentRoot] stringByAppendingPathComponent:@"upload.html"];
    NSDictionary* replacementDict = [NSDictionary dictionaryWithObject:filesStr forKey:@"MyFiles"]; <---------This is where it is used again
    // use dynamic file response to apply our links to response template
    return [[HTTPDynamicFileResponse alloc] initWithFilePath:templatePath forConnection:self separator:@"%" replacementDictionary:replacementDict];
    }
    if( [method isEqualToString:@"GET"] && [path hasPrefix:@"/upload/"] ) {
    // let download the uploaded files
    return [[HTTPFileResponse alloc] initWithFilePath: [[config documentRoot] stringByAppendingString:path] forConnection:self];
    }
   
    return [super httpResponseForMethod:method URI:path];
    }


Reply all
Reply to author
Forward
0 new messages