我从WebKit框架创建了一个自定义浏览器。我几乎完成了所有设置。
然而,当访问带有文件上传的网页(比如flickr)时,按下“上传”按钮时没有任何反应。通常这会在safari / firefox /..
中弹出一个弹出窗口在Cocoa中使用WebKit进行文件上传需要什么? NSFileHandler,NSFileManager?我该怎么做?
此致 Friesgaard
答案 0 :(得分:5)
好吧,我认为我是出于自我。
在类中实现以下方法。在Interface Builder中将类设置为UIDelegate
的{{1}}。
(如何使用我在此处找到的WebView
:
http://ekle.us/index.php/2006/12/displaying_a_file_open_dialog_in_cocoa_w
)
NSOpenPanel
PS。将- (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];
}
}
}
用于多个文件
答案 1 :(得分:3)
我通过改变这一点来实现它,因为表单部分已经折旧:
- (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];
}
}