如何将docx,pdf文件从应用程序主捆绑包上传到服务器?
import UIKit
class UploadFileController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var path:String!
func uploadfiletoserver(){
path = Bundle.main.path(forResource: "mahaligamResume", ofType: "pdf")
print(path)
if let url = URL(string: "https://www.happylifeapps.com/webservices/applyforjobs"){
var request = URLRequest(url: url)
let boundary:String = "Boundary-\(UUID().uuidString)"
// let request = NSMutableURLRequest(url:myUrl! as URL);
request.httpMethod = "POST"
request.timeoutInterval = 10
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var dataFile: Data = Data()
do{
dataFile = try NSData.init(contentsOf: URL(fileURLWithPath: path, isDirectory: true)) as Data
}catch{
print(error)
}
if(dataFile==nil) { return; }
let postJobData:[String:Any] = ["UserId":"1000","JobId":"jobs_14","ResumeOption":"New","CurrentCTC":"12000","PreferredCTC":"15000","CurrentLocation":"Bangalore","PreferredLocation":"Bangalore","Experience":"0","CCTC_sym":"1000","PCTC_sym":"900"]
request.httpBody = createBodyWithParameters(parameters: postJobData, filePathKey: "Resume", FileData: dataFile as NSData , boundary: boundary) as Data
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
print(data)
print(response)
if error != nil {
print("error=\(error)")
return
}else if let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
print("****** response data = \(responseString)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
print(json)
let status = json!["Success"] as! String
let errMessage = json!["Message"] as? String
DispatchQueue.main.async() {
if status == "1"{
print(errMessage)
}else{
print(errMessage)
}
}
}catch{
print(error)
}
}
}; task.resume()
}
}
func createBodyWithParameters(parameters: [String: Any]?, filePathKey: String?, FileData: NSData, boundary: String) -> NSData {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString(string:"--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
}
let filename = path
let mimetype = "pdf/docx"
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(FileData as Data)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body
}
}
extension NSMutableData {
func appendString(string: String) {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)
}
}
我尝试将pdf文件从我的应用程序主捆绑包上传到服务器。但是我在上传时没有出现任何错误,但是当我从服务器收到响应时却出现错误
Message = "The resume must be a file of type: docx, doc or pdf.";
Success = 0;
后端团队指出这是一个编码错误,但我不知道我在哪里出错。请帮我解决这个问题。
答案 0 :(得分:0)
PDF:用于上传pdf文件的PDF的MIME类型和文件名
filename =“ .pdf” mimetype =“ application / pdf”
DOCX:上载docx的模仿类型和文件名 文件名=“ .docx” mimetype =“ application / application / vnd.openxmlformats-officedocument.wordprocessingml.document”
这对我有用,
答案 1 :(得分:0)
要通过本机api上传到服务器的doc文件的文件名和mimetype
func createBodyWithParameters(parameters: [String: Any]?, filePathKey: String?, fileData: NSData, boundary: String) -> NSData {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString(string:"--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
}
filename = ".doc"
mimetype = "application/msword"
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(fileData as Data)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body
}