今天就跟大家聊聊有關怎么在ios中使用AFN上傳文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
- (void)upload{
NSData *imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mine.jpeg" ofType:nil]];
NSDictionary* URLParameters = @{//設置請求頭 };
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"mine.jpeg" ofType:nil]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *repsonse = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];
NSString *mimeType = repsonse.MIMEType;
NSLog(@"%@", repsonse.MIMEType);
[[AFHTTPSessionManager manager] POST:@"上傳網址" parameters:URLParameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:@"mine.jpeg" mimeType:mimeType];
} progress:^(NSProgress * _Nonnull uploadProgress) {
//
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//處理成功
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//處理失敗
}];
}mine.jpeg是在項目存在的圖片,編譯時會以NSBundle的形式存在.首先通過NSURLConnection同步發送請求獲取MIMEType.然后使用AFN,可以將需要上傳的imageData通過方法appendPartWithFileData:放在請求體中,然后傳入已經獲得的MIMEType,就能順利實現上傳了.
看完上述內容,你們對怎么在ios中使用AFN上傳文件有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。