简体   繁体   中英

Cocoa webkit: how to get file upload / file system access in webkit

I created a custom browser from the WebKit framework. I almost got everything setup.

However, when visiting a webpage with file upload (say flickr), nothing happens when I press the "Upload" button. Normally this would give an popup in safari/firefox/..

What do I need to get file upload to work with WebKit in Cocoa? NSFileHandler, NSFileManager? And how do I do it?

Regards, Friesgaard

Alright, I figured I out for my self.

Implement the following method in a class. Set the class as as UIDelegate for the WebView in Interface Builder.

(How to use a NSOpenPanel I found here: http://ekle.us/index.php/2006/12/displaying_a_file_open_dialog_in_cocoa_w )

- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
{       
    // Create the File Open Dialog class.
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    // Enable the selection of files in the dialog.
    [openDlg setCanChooseFiles:YES];

    // Enable the selection of directories in the dialog.
    [openDlg setCanChooseDirectories:NO];

    // Display the dialog.  If the OK button was pressed,
    // process the files.
    if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
    {
        // Get an array containing the full filenames of all
        // files and directories selected.
        NSArray* files = [openDlg filenames];

        // Loop through all the files and process them.
        //for(int i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:0]; //i]; 
            // Do something with the filename.
            [resultListener chooseFilename:fileName]; 
        }
    }

}

ps. use [resultListener chooseFilenames:...] for multiple files

I got it working by altering this a bit as form part is depreciated:

- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
{       
    // Create the File Open Dialog class.
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];

    // Enable the selection of files in the dialog.
    [openDlg setCanChooseFiles:YES];

    // Enable the selection of directories in the dialog.
    [openDlg setCanChooseDirectories:NO];

    if ( [openDlg runModal] == NSOKButton )
    {
        NSArray* URLs = [openDlg URLs];
        NSMutableArray *files = [[NSMutableArray alloc]init];
        for (int i = 0; i <[URLs count]; i++) {
            NSString *filename = [[URLs objectAtIndex:i]relativePath];
            [files addObject:filename];
        }

        for(int i = 0; i < [files count]; i++ )
        {
            NSString* fileName = [files objectAtIndex:i];
            [resultListener chooseFilename:fileName]; 
        }
        [files release];
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM