How to allow file upload using WebView in Cocoa?

WebView has a method called

- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener 

But it has almost 0 documents and details. Inside, I open the open file dialog and get the selected file name.

Like this

 - (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener { NSOpenPanel* openDlg = [NSOpenPanel openPanel]; [openDlg setCanChooseFiles:YES]; [openDlg setCanChooseDirectories:NO]; // process the files. if ( [openDlg runModal] == NSOKButton ) { NSString* fileString = [[openDlg URL]absoluteString]; [resultListener chooseFilename:fileString]; } } 

But then?

What should I do? On the website, it shows that I selected the file, but when you click on the download of the website, just return an error, for example, if the file is not downloaded. Should I write code that handles file upload or what?

I'm a little lost ...

Edit:

Actually I got it working ... Just changing the code from here: Cocoa webkit: how to access the file / file system in webkit a bit, as some part is out of date

 - (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]; } } 

Enjoy it!

+7
source share
2 answers

I followed Peter Hosey's comment and wow, my code is now short and works with the same

 - (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* files = [[openDlg URLs]valueForKey:@"relativePath"]; [resultListener chooseFilenames:files]; } } 
+8
source

There are several ways to improve the code:

  • To iterate over an array, use fast enumeration instead of an index loop. It is faster and easier to read. The only time you have to use an index loop is when you really need indexes, and this is not the case.
  • You do not need the first cycle. Send an array of URLs a valueForKey: using @"relativePath" for the key. The array will query each object (each URL) for its relativePath , collect an array of all the results, and return this for you. The code for this is single-line.
  • You also do not need a second cycle. The WebOpenPanelResultListener protocol added chooseFilenames: in 10.6, so now you can just send this message once, passing it the entire array.
+2
source

All Articles