问题
I am new to iphone, Is it possible to upload uiimage on server without using any php or other files.
I got may examples of uploading image by sending request to some php file. But i don't wants to use php files.
I have server path and Upload folder on server.
Can Anyone has solution to upload image on server with only using iphone ??
Any Idea is greatly appreciated.
回答1:
Where are you uploading a photo to? There needs to be a server side script to receive the file, typically a PHP, Perl, or Python script.
回答2:
func UploadImageWithDict(#Dict : NSMutableDictionary ,Image : UIImage) -> NSMutableDictionary
{
var imageData = UIImageJPEGRepresentation(Image,0.1)
if (imageData != nil)
{
var request = NSMutableURLRequest(URL: NSURL(string:””)!)
var boundary = NSString(format: "---------------------------14737809831466499882746641449")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let body = NSMutableData.alloc()
var postData:String = String()
postData += "--\(boundary)\r\n"
for (key, value : AnyObject) in Dict {
if let value = value as? String {
postData += "--\(boundary)\r\n"
postData += "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n"
postData += "\(value)\r\n"
}
}
postData += "--\(boundary)\r\n"
postData += "Content-Disposition: form-data; name=\"photo\"; filename=\"\(Int64(NSDate().timeIntervalSince1970*1000)).jpg\"\r\n"
postData += "Content-Type: image/jpeg\r\n\r\n"
body.appendData(postData.dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(imageData)
postData = String()
postData += "\r\n"
postData += "\r\n--\(boundary)--\r\n"
body.appendData(postData.dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPMethod = "POST"
request.HTTPBody = body
var Err: AutoreleasingUnsafeMutablePointer<NSError? > = nil
var returnData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:nil , error:Err)
if(Err == nil && returnData != nil)
{
print(">>>>>>>>>")
var returnString = NSString(data: returnData!, encoding: NSUTF8StringEncoding)
var data : NSData? = StringData.dataUsingEncoding(NSUTF8StringEncoding)
var dict : AnyObject? = NSJSONSerialization.JSONObjectWithData(data!,options:NSJSONReadingOptions.AllowFragments, error: nil)
print(dict)
return dict as! NSMutableDictionary
}
else
{
return NSMutableDictionary()
}
}
else
{
return NSMutableDictionary()
}
}
来源:https://stackoverflow.com/questions/4183276/how-to-upload-uiimage-on-server