jin
jin

Reputation: 2155

Get the filepath when dragging a file from finder to a webview?

I am developing a flvplayer with cocoa. I want to drag a flv file from the finder and drop it to the webview window (I use it to view the flv). But I didn't have the path of this file from finder. This is my code:

 _ (void)showflv(nsstring*)aa
{               
    ..........    
    //Register to accept flv drag/drop    
    NSString *pboardType = NSCreateFileContentsPboardType(@"flv");
    NSArray *dragTypes = [NSArray arrayWithObject:pboardType];
    [self registerForDraggedTypes:dragTypes];
}

 _ (BOOL)performDragOperation:(id <NSDraggingInfo>)sender    
{
    NSPasteboard *pboard = [sender draggingPasteboard];     
    if ( [[pboard types] containsObject:NSFileContentsPboardType] ) {
    }
    return YES;
}

I read the document about this.I find that the function of PerformDragOperation can be written like this:

_ (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];

    if ( [[pboard types] containsObject:NSFileContentsPboardType] ) {
        NSFileWrapper *fileContents = [pboard readFileWrapper];
        // Perform operation using the file’s contents
    }
    return YES;
}

I can get the filecontents of this dragging file.but how to get the path?

NSString *fileName = [fileContents filename];

I did a test, it wasn't correct.


I did a test ,but The draggedFilePaths didn't work.I found that the code:

NSPasteboard *pboard = [sender draggingPasteboard];

if ( [[pboard types] containsObject:NSFileContentsPboardType] ) {
    //NSFileWrapper *fileContents = [pboard readFileWrapper];
    // Perform operation using the file’s contents
    nslog(@"aa");
}

when the app run ,the string "aa" didn't appear,why?

Upvotes: 2

Views: 2668

Answers (2)

Lindemann
Lindemann

Reputation: 3396

NSPasteboard *pastboard = [sender draggingPasteboard];

NSURL *URL = [NSURL URLFromPasteboard:pastboard];

NSLog(@"%@", URL);

Upvotes: 1

Chuck
Chuck

Reputation: 237010

Like so:

NSArray *draggedFilePaths = [pboard propertyListForType:NSFilenamesPboardType];

Upvotes: 8

Related Questions