Skip to content

File upload doesn't use file name #98

Closed
zachwaugh opened this Issue Nov 1, 2011 · 5 comments

2 participants

@zachwaugh

When uploading a file with - (void)appendPartWithFileData:(NSData *)data mimeType:(NSString *)mimeType name:(NSString *)name, it replaces the actual name of the file with a name based on the current time and mime type

NSString *fileName = [[NSString stringWithFormat:@"%@-%d", name, [[NSDate date] hash]] stringByAppendingPathExtension:[mimeType lastPathComponent]];

I can see the benefit of this on iOS, when uploading something like a photo that might not have a real file name, but in general, I would want the actual file name to be sent with the upload. Also, it be really nice to have mime type automatically detected as well.

@mattt
mattt commented Nov 1, 2011

Previously, AFMultipartFormData did have the method - (void)appendPartWithFile:mimeType:fileName:error:, but I pulled it because it was being confused with the file data version. But yeah—thanks for your suggestion. I'll tinker around to figure out the best way to add that functionality.

@zachwaugh

Thanks! Currently converting my app from ASI to AFNetworking, and loving it so far.

For that API, I'd love a convenience method like - (void)appendPartWithFile:(NSString *)path name:(NSString *)name that automatically figured out the mime type and file name, and pulled the NSData object from the file. Maybe that calls a lower-level method like this, for when you need complete control:

- (void)appendPartWithFileData:(NSData *)data fileName:(NSString *)fileName mimeType:(NSString *)mimeType name:(NSString *)name

If mime type is nil, automatically detect it. If fileName is nil, create it like the current method. That be the best of both worlds (for me at least).

@mattt mattt added a commit that referenced this issue Nov 7, 2011
@mattt mattt [Issue #98] Adding AFHTTPClient -appendPartWithFileURL:name:error:
Changing -appendPartWithFileData:mimeType:name: to -appendPartWithFileData:name:fileName:mimeType:
9a91afe
@mattt
mattt commented Nov 7, 2011

9a91afe Adds - (void)appendPartWithFileURL:(NSURL *)fileURL name:(NSString *)name error:(NSError **)error, which should do pretty much exactly what you were looking for (I hope). I also changed the other file data method to take a filename argument, rather than auto-generating one. Let me know how that works for you!

@zachwaugh

Thanks, those methods are exactly what I was looking for. Just tried it out and worked perfectly.

@mattt
mattt commented Nov 9, 2011

Awesome! So glad to hear it. Thanks for opening this issue—really glad to have this new method in there.

@mattt mattt closed this Nov 9, 2011
@egrim egrim pushed a commit to egrim/AFNetworking that referenced this issue Sep 18, 2012
@mattt mattt [Issue #98] Adding AFHTTPClient -appendPartWithFileURL:name:error:
Changing -appendPartWithFileData:mimeType:name: to -appendPartWithFileData:name:fileName:mimeType:
d85bbbd
@greghe greghe pushed a commit to skillz/AFNetworking that referenced this issue Sep 3, 2015
@mattt mattt [Issue #98] Adding AFHTTPClient -appendPartWithFileURL:name:error:
Changing -appendPartWithFileData:mimeType:name: to -appendPartWithFileData:name:fileName:mimeType:
6317144
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.