【发布时间】:2017-02-05 13:45:57
【问题描述】:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {
var window: UIWindow?
var manager1: SessionManager?
var manager2: SessionManager?
let commons:Commons = Commons()
var selected = 0
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
func uploade()
{
if(selected == 0)
{
let realm = try! Realm()
let uploadAuditInfo = realm.objects(UploadAudit.self)
var uploadAudit = [UploadAudit]()
let uploadDistInfo = realm.objects(UploadData.self)
var uploadDist = [UploadData]()
for i in 0 ..< uploadAuditInfo.count {
if let result = uploadAuditInfo[i] as? UploadAudit {
uploadAudit.append(result)
}
}
for i in 0 ..< uploadDistInfo.count {
if let result = uploadDistInfo[i] as? UploadData {
uploadDist.append(result)
}
}
if(uploadAudit.count > 0)
{
selected = 1
let upload = uploadAudit[0]
let fileNameToDelete = upload.fileName
var filePath = ""
let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0 {
let dir = dirs[0] //documents directory
filePath = dir.appendingFormat("/" + fileNameToDelete)
} else {
return
}
let lang = self.commons.getLanguage()
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath)
{
let image = UIImage(contentsOfFile: filePath)
let ids = upload.id
let params = upload.params
let name1 = upload.fileName
let url = self.commons.getUrl()
let vas1 = lang+params
let vas2 = url+"rest/secure/"+String(ids)+"/submit?language="
let vas = vas2+vas1
print("upload audit111")
uploadFile(fileName: name1, mimeType:"application/octed-stream", image: image!, url:vas, id: ids)
}
else
{
try! realm.write {
realm.delete(upload)
}
selected = 0
self.uploade()
}
}
else if(uploadDist.count > 0)
{
selected = 1
let upload = uploadDist[0]
let fileNameToDelete = upload.fileName
var filePath = ""
let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0 {
let dir = dirs[0] //documents directory
filePath = dir.appendingFormat("/" + fileNameToDelete)
} else {
return
}
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath)
{
let lang = self.commons.getLanguage()
let image = UIImage(contentsOfFile: filePath)
let ids = upload.id
let params = upload.params
let name1 = upload.fileName
let url = self.commons.getUrl()
let vas1 = lang+params
let vas2 = url+"rest/secure/another/"+String(ids)+"/submit?language="
let vas = vas2+vas1
print("upload dist111")
uploadFileDist(fileName: name1, mimeType:"application/octed-stream", image: image!, url:vas, id: ids)
}
else
{
try! realm.write {
realm.delete(upload)
}
selected = 0
self.uploade()
}
}
else
{
}}
else
{
}
}
func uploadFileDist(fileName: String, mimeType: String, image: UIImage, url: String, id: Int )
{
let preferences = UserDefaults.standard
//NSUserDefaults.standardUserDefaults()
let currentLevelKey = "sessionId"
if UserDefaults.standard.object(forKey: currentLevelKey) == nil {
} else {
let name = preferences.string(forKey: currentLevelKey)
DispatchQueue.global().async {
let sessionName = "JSESSIONID="+name!
// get headers
let headers = self.commons.getHeader(cookie: sessionName)
// get language
// get configuration
let configuration = self.commons.getConfiguration(timeout: 600)
self.manager2 = Alamofire.SessionManager(configuration: configuration)
// get base url
let image_data = UIImageJPEGRepresentation(image, 0.4)
let uuid = UUID().uuidString
let uuid2 = UUID().uuidString
let nameOfImage = "profile-"+uuid+"--"+uuid2
print("dist upload")
self.manager2!.upload(multipartFormData: { (MultipartFormData) in
MultipartFormData.append(image_data!, withName: "uploadFile", fileName: nameOfImage+".jpeg", mimeType: mimeType)
}, to: url, method: .post, headers: headers, encodingCompletion: { (result) in
//self.manager1!.session.invalidateAndCancel()
print("dist result")
switch result {
case .success( let upload , _, _):
upload.responseObject(keyPath: ""){ (response: DataResponse<User>) in
let realm = try! Realm()
let ps = "id == "+String(id)
let datas = realm.objects(UploadData.self).filter(ps)
let datas2 = realm.objects(DistRealM.self).filter(ps)
try! realm.write {
realm.delete(datas)
realm.delete(datas2)
}
self.selected = 0
self.uploade()
}
case .failure( _):
self.selected = 0
}
})
}
}
}
func uploadFile(fileName: String, mimeType: String, image: UIImage, url: String, id: Int )
{
let preferences = UserDefaults.standard
//NSUserDefaults.standardUserDefaults()
let currentLevelKey = "sessionId"
if UserDefaults.standard.object(forKey: currentLevelKey) == nil {
} else {
let name = preferences.string(forKey: currentLevelKey)
DispatchQueue.global().async {
// set session cookie
let sessionName = "JSESSIONID="+name!
// get headers
let headers = self.commons.getHeader(cookie: sessionName)
// get language
// get configuration
let configuration = self.commons.getConfiguration(timeout: 600)
self.manager2 = Alamofire.SessionManager(configuration: configuration)
// get base url
print(" audit upload ")
let image_data = UIImageJPEGRepresentation(image, 0.4)
let uuid = UUID().uuidString
let uuid2 = UUID().uuidString
let nameOfImage = "profile-"+uuid+"--"+uuid2
self.manager2!.upload(multipartFormData: { (MultipartFormData) in
MultipartFormData.append(image_data!, withName: "uploadFile", fileName: nameOfImage+".jpeg", mimeType: mimeType)
}, to: url, method: .post, headers: headers, encodingCompletion: { (result) in
// self.manager1!.session.invalidateAndCancel()
print("audit result")
switch result {
case .success( let upload, _, _):
upload.responseObject(keyPath: ""){ (response: DataResponse<User>) in
debugPrint(response)
let realm = try! Realm()
let ps = "id == "+String(id)
let datas = realm.objects(UploadAudit.self).filter(ps)
let datas2 = realm.objects(AuditRealM.self).filter(ps)
try! realm.write {
realm.delete(datas)
realm.delete(datas2)
}
self.selected = 0
self.uploade()
}
case .failure( _):
self.selected = 0
}
})
}
}}}
我在 swift 3 中使用 alamofire lirbary 执行此操作,同样的调用在其他地方完美运行。但是在这里,我不断收到 Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=
为什么我的 url 调用总是无缘无故地被取消?同样的调用在其他地方完美运行。
【问题讨论】: