WebView có một phương pháp gọi làLàm cách nào để cho phép tải lên tệp bằng WebView trong Cocoa?
- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id <WebOpenPanelResultListener>)resultListener
Nhưng hầu như 0 doc và chi tiết về nó. Bên trong tôi hiển thị một hộp thoại mở tập tin và nhận được tên tập tin được chọn.
Như thế này
- (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];
}
}
Nhưng sau đó?
Tôi nên làm gì? Trên trang web, nó cho thấy rằng tôi đã chọn một tệp nhưng khi bạn nhấp vào tải lên, trang web chỉ trả lại lỗi, như thể không có tệp nào được tải lên. Tôi có nên viết mã xử lý tệp tải lên hay không?
Tôi kinda mất ...
Edit:
Trong thực tế, tôi đã nhận nó làm việc .... Bởi chỉ cần thay đổi mã từ đây: Cocoa webkit: how to get file upload/file system access in webkit một chút, như một số phần đã bị phản đối
- (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];
}
}
Tận hưởng!
gì là ngôn ngữ mà bạn đang sử dụng? –
Mục tiêu-c, nhưng bây giờ tôi đã làm việc – Dimillian
rồi xóa câu hỏi của bạn .. –