把它在您的评论如下,你告诉我们,你使用的是$_FILES
语法来检索文件。这意味着你想创建一个multipart/form-data
请求。该过程基本上是:
指定您的multipart/form-data
请求的边界。
指定请求的Content-Type
指定它multipart/form-data
以及边界是什么。
创建请求的主体,分离各个组件(每个发布的值以及每个上传之间)。
有关更多详细信息,请参见RFC 2388。不管怎样,在斯威夫特3,这可能是这样的:
/// Create request
///
/// - parameter userid: The userid to be passed to web service
/// - parameter password: The password to be passed to web service
/// - parameter email: The email address to be passed to web service
///
/// - returns: The `URLRequest` that was created
func createRequest(userid: String, password: String, email: String) throws -> URLRequest {
let parameters = [
"user_id" : userid,
"email" : email,
"password" : password] // build your dictionary however appropriate
let boundary = generateBoundaryString()
let url = URL(string: "https://example.com/imageupload.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let path1 = Bundle.main.path(forResource: "image1", ofType: "png")!
request.httpBody = try createBody(with: parameters, filePathKey: "file", paths: [path1], boundary: boundary)
return request
}
/// Create body of the `multipart/form-data` request
///
/// - parameter parameters: The optional dictionary containing keys and values to be passed to web service
/// - parameter filePathKey: The optional field name to be used when uploading files. If you supply paths, you must supply filePathKey, too.
/// - parameter paths: The optional array of file paths of the files to be uploaded
/// - parameter boundary: The `multipart/form-data` boundary
///
/// - returns: The `Data` of the body of the request
private func createBody(with parameters: [String: String]?, filePathKey: String, paths: [String], boundary: String) throws -> Data {
var body = Data()
if parameters != nil {
for (key, value) in parameters! {
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.append("\(value)\r\n")
}
}
for path in paths {
let url = URL(fileURLWithPath: path)
let filename = url.lastPathComponent
let data = try Data(contentsOf: url)
let mimetype = mimeType(for: path)
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(filePathKey)\"; filename=\"\(filename)\"\r\n")
body.append("Content-Type: \(mimetype)\r\n\r\n")
body.append(data)
body.append("\r\n")
}
body.append("--\(boundary)--\r\n")
return body
}
/// Create boundary string for multipart/form-data request
///
/// - returns: The boundary string that consists of "Boundary-" followed by a UUID string.
private func generateBoundaryString() -> String {
return "Boundary-\(UUID().uuidString)"
}
/// Determine mime type on the basis of extension of a file.
///
/// This requires `import MobileCoreServices`.
///
/// - parameter path: The path of the file for which we are going to determine the mime type.
///
/// - returns: Returns the mime type if successful. Returns `application/octet-stream` if unable to determine mime type.
private func mimeType(for path: String) -> String {
let url = URL(fileURLWithPath: path)
let pathExtension = url.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream"
}
有了:
extension Data {
/// Append string to Data
///
/// Rather than littering my code with calls to `data(using: .utf8)` to convert `String` values to `Data`, this wraps it in a nice convenient little extension to Data. This defaults to converting using UTF-8.
///
/// - parameter string: The string to be added to the `Data`.
mutating func append(_ string: String, using encoding: String.Encoding = .utf8) {
if let data = string.data(using: encoding) {
append(data)
}
}
}
或者,斯威夫特2:
/// Create request
///
/// - parameter userid: The userid to be passed to web service
/// - parameter password: The password to be passed to web service
/// - parameter email: The email address to be passed to web service
///
/// - returns: The NSURLRequest that was created
func createRequest (userid userid: String, password: String, email: String) -> NSURLRequest {
let param = [
"user_id" : userid,
"email" : email,
"password" : password] // build your dictionary however appropriate
let boundary = generateBoundaryString()
let url = NSURL(string: "https://example.com/imageupload.php")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let path1 = NSBundle.mainBundle().pathForResource("image1", ofType: "png") as String!
request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", paths: [path1], boundary: boundary)
return request
}
/// Create body of the multipart/form-data request
///
/// - parameter parameters: The optional dictionary containing keys and values to be passed to web service
/// - parameter filePathKey: The optional field name to be used when uploading files. If you supply paths, you must supply filePathKey, too.
/// - parameter paths: The optional array of file paths of the files to be uploaded
/// - parameter boundary: The multipart/form-data boundary
///
/// - returns: The NSData of the body of the request
func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, paths: [String]?, boundary: String) -> NSData {
let body = NSMutableData()
if parameters != nil {
for (key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
if paths != nil {
for path in paths! {
let url = NSURL(fileURLWithPath: path)
let filename = url.lastPathComponent
let data = NSData(contentsOfURL: url)!
let mimetype = mimeTypeForPath(path)
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename!)\"\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.appendData(data)
body.appendString("\r\n")
}
}
body.appendString("--\(boundary)--\r\n")
return body
}
/// Create boundary string for multipart/form-data request
///
/// - returns: The boundary string that consists of "Boundary-" followed by a UUID string.
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().UUIDString)"
}
/// Determine mime type on the basis of extension of a file.
///
/// This requires MobileCoreServices framework.
///
/// - parameter path: The path of the file for which we are going to determine the mime type.
///
/// - returns: Returns the mime type if successful. Returns application/octet-stream if unable to determine mime type.
func mimeTypeForPath(path: String) -> String {
let url = NSURL(fileURLWithPath: path)
let pathExtension = url.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream";
}
和:
extension NSMutableData {
/// Append string to NSMutableData
///
/// Rather than littering my code with calls to `dataUsingEncoding` to convert strings to NSData, and then add that data to the NSMutableData, this wraps it in a nice convenient little extension to NSMutableData. This converts using UTF-8.
///
/// - parameter string: The string to be added to the `NSMutableData`.
func appendString(string: String) {
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
appendData(data!)
}
}
有所有这些,你现在需要提交这个请求。我建议不要在你的问题中使用同步技术。你应该这样做异步。例如,在URLSession
,斯威夫特3,你会做这样的事情:
let request: URLRequest
do {
request = try createRequest(userid: userid, password: password, email: email)
} catch {
print(error)
return
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard error == nil else {
// handle error here
print(error!)
return
}
// if response was JSON, then parse it
do {
let responseDictionary = try JSONSerialization.jsonObject(with: data!)
print("success == \(responseDictionary)")
// note, if you want to update the UI, make sure to dispatch that to the main queue, e.g.:
//
// DispatchQueue.main.async {
// // update your UI and model objects here
// }
} catch {
print(error)
let responseString = String(data: data!, encoding: .utf8)
print("responseString = \(responseString)")
}
}
task.resume()
或者,雨燕2再现:
let request = createRequest(userid: userid, password: password, email: email)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
if error != nil {
// handle error here
print(error)
return
}
// if response was JSON, then parse it
do {
if let responseDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
print("success == \(responseDictionary)")
// note, if you want to update the UI, make sure to dispatch that to the main queue, e.g.:
//
// dispatch_async(dispatch_get_main_queue()) {
// // update your UI and model objects here
// }
}
} catch {
print(error)
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
}
task.resume()
我原来的答复是低于历史的目的:
几点意见:
您正在将HTTPBody
设置为标准POST
格式(就好像它是application/x-www-form-urlencoded
请求,即使您从未指定过)。然后,您继续放弃,并将其替换为图像的PNG表示的二进制数据。你大概想要发送两个。
我们不能就恰恰是服务器期待着与澄清劝你,但是通常是multipart/form-data
,而不是application/x-www-form-urlencoded
(例如,如果它是一个PHP Web服务,它使用$_FILES
变量)。如果您正在尝试执行multipart/form-data
,请参阅此文档,例如POST multipart/form-data with Objective-C,例如如何操作。显然这是Objective-C,但它说明了基本技术。
请注意,还有其他格式的其他网络服务使用,所以我犹豫,只是假设这是预计multipart/form-data
请求。您应该确切确认服务器的预期结果。
不用说,还有其他一些问题,太(例如,你真的应该指定请求的Content-Type
,起码,你真的不应该发出同步请求(除非你已经做这在后台线程);我可能会建议NSURLSession
;等等)。
但主要问题是你如何填充HTTPBody
。尽管如此,我们很难帮助您进一步了解服务器需求。
你有什么问题'filePathKey'我面临同样的问题。 – 2015-04-13 20:47:22
上传图片简单... [使用Alamofire](http://stackoverflow.com/questions/32366021/alamofire-error-code-1000-the-url-does-not-point-to-a-file-url/ 36863066#36863066) – 2016-04-26 12:01:38