|
|
@@ -123,41 +123,82 @@
|
|
|
//
|
|
|
|
|
|
if (self.isDownloadTask) {
|
|
|
- [self.fileStream jl_closeWriter];
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- printf("net work complete\n");
|
|
|
- if (self.r) {
|
|
|
|
|
|
+ [self.fileStream jl_closeWriter];
|
|
|
|
|
|
- if(self.recvData==nil)
|
|
|
- {
|
|
|
- self.r(nil);
|
|
|
- return;
|
|
|
+ if (error) {
|
|
|
+ if (error.code != -999) {
|
|
|
+ if (self.r) {
|
|
|
+
|
|
|
+ NSMutableDictionary *resutlDic = [@{
|
|
|
+ @"result" : @0,
|
|
|
+ @"msg" : error.localizedDescription
|
|
|
+ } mutableCopy];
|
|
|
+ self.r(resutlDic);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ NSLog(@"download task canceled");
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
+ // 失败删除文件
|
|
|
+ BOOL isDir = NO;
|
|
|
+ if ([[NSFileManager defaultManager] fileExistsAtPath:self.fileCachePath isDirectory:&isDir]) {
|
|
|
+
|
|
|
+ if (!isDir) {
|
|
|
+ [[NSFileManager defaultManager] removeItemAtPath:self.fileCachePath error:nil];
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ if (self.r) {
|
|
|
+
|
|
|
+ NSMutableDictionary *resutlDic = [@{
|
|
|
+ @"result" : @2,
|
|
|
+ @"path" : self.fileCachePath,
|
|
|
+ @"msg" : @"download complete"
|
|
|
+ } mutableCopy];
|
|
|
+ self.r(resutlDic);
|
|
|
+ }
|
|
|
}
|
|
|
+ } else {
|
|
|
|
|
|
- NSMutableString *str = [[NSMutableString alloc] initWithData:self.recvData encoding:NSUTF8StringEncoding];
|
|
|
-
|
|
|
- NSLog(@"data string: %@",str);
|
|
|
-
|
|
|
- NSError *error1=nil;
|
|
|
- NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:self.recvData options:NSJSONReadingMutableLeaves error:&error1];
|
|
|
- if(jsobj==nil)// 服务器返回不是json
|
|
|
- {
|
|
|
+ if (self.r) {
|
|
|
|
|
|
- jsobj=[NSMutableDictionary new];
|
|
|
- [jsobj setValue:@"-30" forKey:@"result"];
|
|
|
- [jsobj setValue:@"Can not upload to server, please contact administrator." forKey:@"msg"];
|
|
|
|
|
|
+ if(self.recvData==nil)
|
|
|
+ {
|
|
|
+ self.r(nil);
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
+ NSMutableString *str = [[NSMutableString alloc] initWithData:self.recvData encoding:NSUTF8StringEncoding];
|
|
|
+
|
|
|
+ NSLog(@"data string: %@",str);
|
|
|
+
|
|
|
+ NSError *error1=nil;
|
|
|
+ NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:self.recvData options:NSJSONReadingMutableLeaves error:&error1];
|
|
|
+ if(jsobj==nil)// 服务器返回不是json
|
|
|
+ {
|
|
|
+
|
|
|
+ jsobj=[NSMutableDictionary new];
|
|
|
+ [jsobj setValue:@"-30" forKey:@"result"];
|
|
|
+ [jsobj setValue:@"Can not upload to server, please contact administrator." forKey:@"msg"];
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ self.r([jsobj mutableCopy]);
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- self.r([jsobj mutableCopy]);
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
+ printf("net work complete\n");
|
|
|
+
|
|
|
//
|
|
|
[session invalidateAndCancel];
|
|
|
}
|
|
|
@@ -179,8 +220,11 @@
|
|
|
BOOL isDir = NO;
|
|
|
[[NSFileManager defaultManager] fileExistsAtPath:desPath isDirectory:&isDir];
|
|
|
if (isDir) {
|
|
|
-
|
|
|
- desPath = [self.fileCachePath stringByAppendingPathComponent:response.suggestedFilename];
|
|
|
+ if (response.suggestedFilename) {
|
|
|
+ desPath = [self.fileCachePath stringByAppendingPathComponent:response.suggestedFilename];
|
|
|
+ } else {
|
|
|
+ desPath = [self.fileCachePath stringByAppendingPathComponent:[NSUUID UUID].UUIDString];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 如果文件存在
|
|
|
@@ -209,6 +253,14 @@
|
|
|
[self.recvData appendData:data];
|
|
|
} else {
|
|
|
|
|
|
+
|
|
|
+ if (self.p) {
|
|
|
+ int64_t totalExpectedRevc = [dataTask countOfBytesExpectedToReceive];
|
|
|
+ int64_t totalRevc = [dataTask countOfBytesReceived];
|
|
|
+ double progress = (double)totalRevc / totalExpectedRevc;
|
|
|
+ self.p(dataTask,progress);
|
|
|
+ }
|
|
|
+
|
|
|
[self.fileStream jl_writeData:data];
|
|
|
|
|
|
}
|