如何在Cocoa中允许使用WebView上传文件?
WebView 有一个名为
- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
But 的方法,但几乎没有 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];
}
}
但是然后呢?
我应该怎么办 ?在网站上,它显示我选择了一个文件,但是当您单击上传时,网站只返回一个错误,就像没有上传文件一样。我应该编写处理文件上传的代码还是什么?
我有点迷路了...
编辑:
事实上我让它工作了...只需从这里更改代码: Cocoa webkit:如何获取文件上传/文件系统在 webkit 中访问一下,因为某些部分已被弃用,
- (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];
}
}
享受吧!
WebView have a method called
- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
But there is almost 0 doc and details on it. Inside I display an 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 website, it show that I selected a file, but when you click on upload the website just return an error, like if no file is uploaded. Should I write the code that handle the file upload or what ?
I'm kinda lost...
Edit:
In fact I got it working.... By just alter the code from here: Cocoa webkit: how to get file upload / file system access in webkit a bit, as some part was deprecated
- (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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我关注了 Peter Hosey 的评论,哇,我的代码现在很短并且工作原理相同
I followed Peter Hosey comment and wow, my code is now short and works the same
有几种方法可以改进您的代码:
valueForKey:
消息,其中@"relativePath"
作为键。该数组将向每个对象(每个 URL)询问其relativePath
,收集所有结果的数组,然后将其返回给您。此代码是一行代码。WebOpenPanelResultListener
协议在 10.6 中添加了chooseFilenames:
,因此您现在只需发送该消息一次,即可将整个数组传递给它。There are a couple of ways your code can be improved:
valueForKey:
message, with@"relativePath"
for the key. The array will ask each object (each URL) for itsrelativePath
, collect an array of all the results, and return that for you. The code for this is a one-liner.WebOpenPanelResultListener
protocol addedchooseFilenames:
in 10.6, so you can now just send that message, once, passing the whole array to it.