作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 iOS 应用程序开发新手,我只想使用 NSMutableURLRequest 上传图像文件。请求正文应包含在图像中。我只想将如下图像作为java代码发送
HttpPut mCreatePost = new HttpPut(params[0]+"/data");
FileEntity fileEntity = new FileEntity(uploadFile, "image/jpeg");
mCreatePost.setEntity(fileEntity);
mHttpClient.execute(mCreatePost);
请帮我写一下上面的 Objective C 代码。提前致谢。
最佳答案
流程
示例代码
- (void)uploadImage
NSString *urlStr = @"your link to the server";
NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setURL:[NSURL URLWithString:urlStr]];
[urlRequest setHTTPMethod:@"POST"];
NSString *myboundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",myboundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postData = [NSMutableData data];
NSString *filePath = @"path to file you want to upload";
NSData *zipData = [NSData dataWithContentsOfFile:filePath];
if(zipData != nil){
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", @"file.zip"]dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:zipData]];
}
NSString *params =@"Any other parameters you want to send with the file";
NSLog(@"%@",params);
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"params"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"%@", params] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postData];
[urlRequest setValue:[SettingsDataStore getSharedInstance].cooike forHTTPHeaderField:@"Cookie"];
responsedata = [[NSMutableData alloc] initWithData:nil];
conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}
//NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responsedata appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"error occured %@", [error localizedDescription]);
[responseData release];
[conn release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//Do something with responseData and release it
[responseData release];
[conn release];
}
关于java - Objective C iOS 中文件上传图像文件代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15657666/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!