如何允许在Cocoa中使用WebView上载文件?
WebView有一个名为如何允许在Cocoa中使用WebView上载文件?,cocoa,file,upload,webview,uploading,Cocoa,File,Upload,Webview,Uploading,WebView有一个名为 - (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener -(void)webView:(webView*)发件人runOpenPanelForFileButtonWithResultListener:(id)resultListener 但是几乎有0个文档和
- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
-(void)webView:(webView*)发件人runOpenPanelForFileButtonWithResultListener:(id)resultListener
但是几乎有0个文档和详细信息。在内部,我显示一个“打开文件”对话框,并获取选定的文件名
像这样
- (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];
}
}
-(void)webView:(webView*)发件人runOpenPanelForFileButtonWithResultListener:(id)resultListener
{
NSOpenPanel*openDlg=[NSOpenPanel-openPanel];
[openDlg setCanChooseFiles:是];
[openDlg SetCanChoosedDirectories:否];
//处理文件。
if([openDlg runModal]==NSOKButton)
{
NSString*fileString=[[openDlg URL]绝对字符串];
[resultListener chooseFilename:fileString];
}
}
但是后来呢
我该怎么办?在网站上,它显示我选择了一个文件,但当你点击上传网站只是返回一个错误,如果没有文件上传。我应该写处理文件上传的代码吗
我有点迷路了
编辑:
事实上我让它工作了。。。。只需从这里修改代码:稍微修改一下,因为某些部分已被弃用
- (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];
}
}
-(void)webView:(webView*)发件人runOpenPanelForFileButtonWithResultListener:(id)resultListener
{
//创建文件打开对话框类。
NSOpenPanel*openDlg=[NSOpenPanel-openPanel];
//启用对话框中的文件选择。
[openDlg setCanChooseFiles:是];
//启用对话框中的目录选择。
[openDlg SetCanChoosedDirectories:否];
if([openDlg runModal]==NSOKButton)
{
NSArray*URL=[openDlg URL];
NSMutableArray*文件=[[NSMutableArray alloc]init];
对于(int i=0;i,有几种方法可以改进代码:
- 要在数组中循环,请使用而不是索引循环。它不仅更快,而且更易于读取。唯一应该使用索引循环的时间是在确实需要索引时,而不是在这种情况下
- 您根本不需要第一个循环。向URL数组发送一条
valueForKey:
消息,其中键为@“relativePath”
。该数组将向每个对象(每个URL)请求其relativePath
,收集所有结果的数组,然后为您返回该结果。该数组的代码是一行代码
- 您也不需要第二个循环。在10.6中添加了
WebOpenPanelResultListener
协议chooseFilenames:
,因此您现在只需发送该消息一次,然后将整个数组传递给它
我跟踪了Peter Hosey的评论,哇,我的代码现在很短,工作原理也一样
- (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];
}
}
-(void)webView:(webView*)发件人runOpenPanelForFileButtonWithResultListener:(id)resultListener
{
//创建文件打开对话框类。
NSOpenPanel*openDlg=[NSOpenPanel-openPanel];
//启用对话框中的文件选择。
[openDlg setCanChooseFiles:是];
//启用对话框中的目录选择。
[openDlg SetCanChoosedDirectories:否];
if([openDlg runModal]==NSOKButton)
{
NSArray*文件=[[openDlg URL]valueForKey:@“relativePath”];
[结果列表选择文件名:文件];
}
}
您使用的语言是什么?Objective-c,但我现在已经可以使用了,或者,将解决方案作为答案发布,然后接受您自己的答案(如果有更好的答案,可以使用更好的答案)later.runOpenPanelForFileButtonWithResultListener此方法不太适用于甲烷,我真的需要学习使用Objective CrunOpenPanelForFileButtonWithResultListener的真正威力,此方法不适用于甲烷me@jkk如果其他人遇到此问题,则需要将视图连接到“UIDelegate”,否则该函数将无法调用带路。