我无法将pdf文件从ios设备上传到服务器(PHP)。我知道我的PHP代码有效,因为我用它来接收来自Web客户端的上传。以下是我的代码:
上传新创建的空白PDF文档的功能:
let uploadURL = "https://www.example.com/myUplpoadFile.php"
@IBAction func createNewFolder(_ sender: Any) {
print("createNewFolder pressed ...")
var fileName = nameFolder.text
fileName = fileName! + ".pdf"
// Create new PDFDocument
let newDoc = PDFDocument.init()
// Write to cache directory
let newDocURL = generateURLForDocument(nameOfFile: fileName!)
newDoc.write(to: newDocURL)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(newDocURL, withName: "file1")
},
to: uploadURL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
}
// Create URL for PDFDocument to write to memory(Library/Caches/)
func generateURLForDocument(nameOfFile: String) -> URL {
let cacheDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
let urlFile = cacheDir.appendingPathComponent(nameOfFile)
return urlFile
}
PHP(AWS文件上传):
$tempPath = $_FILES["file1"]['tmp_name'];
$fileName = $_FILES["file1"]['name'];
$bucketName = 'myAWSBucket';
$folder = 'UploadFiles';
$userId = $_REQUEST['user_id'];
$userFolder = 'user_'.$userId;
$userLabBookFolder = 'myBook';
$pathToLabBook = $folder.'/'.$userFolder.'/'.$userLabBookFolder.'/'.$fileName;
try{
$result = $client -> putObject(array(
'ACL' => 'public-read',
'Bucket' => $bucketName,
'Key' => $pathToLabBook,
'SourceFile' => $tempPath
));
// Remove temp file
unlink($tempPath);
}catch (S3Exception $e) {
die('Error:' . $e->getMessage());
} catch (Exception $e) {
die('Error:' . $e->getMessage());
}
从控制台输出:
[Response]: <NSHTTPURLResponse: 0x6000002292e0> { URL: https://www.example/myUplpoadFile.php } { Status Code: 200, Headers {
"Content-Length" = (
268
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
Date = (
"Wed, 14 Mar 2018 12:24:04 GMT"
);
Server = (
"Apache/2.4.27 (Amazon) OpenSSL/1.0.2k-fips PHP/7.0.27"
);
"x-powered-by" = (
"PHP/7.0.27"
);
} }
[数据]:268 字节[结果]:FAILURE:serializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误 Domain = NSCocoaErrorDomain Code = 3840“字符周围的值无效 0.“UserInfo = {NSDebugDescription =字符0周围的值无效。}))[时间轴]:时间线:{”请求开始时间“:542723043.940,”初始值 响应时间“:542723044.323,”请求完成时间“: 542723044.539,“序列化完成时间”:542723044.540,“延迟”:0.383秒,“请求持续时间”:0.599秒,“序列化 持续时间“:0.000秒,”总持续时间“:0.599秒} 2018-03-14 12:25:04.646785 + 0000 labbook [13909:1338114] [BoringSSL]功能 boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为连接是 使用close_notify警报2018-03-14彻底关闭 12:25:04.647073 + 0000 labbook [13909:1338114] [BoringSSL]功能 boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为连接是 使用close_notify警报干净地关闭