• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 将两张图片上传到 WebServer

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:54:43 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试将两张图片上传到我的网络服务器。这是我现在要做的,上传一张图片:

NSData *imageData = UIImagePNGRepresentation(imageToSend);
// setting up the URL to post to
NSString *urlString = @"http://www.myweb.com.br/_resources/testeDir.php";

// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod"OST"];

NSString *boundary = @"14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
 now lets create the body of the post
 */
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

//Create the connection with the string URL and kick it off
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[urlConnection start];

一张图片效果很好,但是,我怎么能同时发送两张图片呢?

一种选择是使用 AFNetworking,但到目前为止我无法让它工作,这是我尝试过的:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:urlString];
NSMutableURLRequest *requestHTTP = [httpClient multipartFormRequestWithMethod"OST" path"/" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name"userfile" fileName"ipodfile.jpg" mimeType"image/jpeg"];
}];

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:requestHTTP];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
    //CGFloat progress = ((CGFloat)totalBytesWritten) / totalBytesExpectedToWrite * 100;


}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Upload succes");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Upload failed");
}];
[operation start];

这是我的 php 代码:

<?php
    $currentDirectory = getcwd(); 
    $uploaddir = $currentDirectory."/4/HD/";
    $uploaddir2 = $currentDirectory."/4/thumb/";
    $file = basename($_FILES['userfile']['name']);
    $uploadfile = $uploaddir . $file;
    $file2 = basename($_FILES['userfile2']['name2']);
    $uploadfile2 = $uploaddir2 . $file2;

  if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo $uploaddir."{$file}";
      } 

  if (move_uploaded_file($_FILES['userfile2']['tmp_name'], $uploadfile2)) {
        echo $uploaddir2."{$file2}";
      } 
?>



Best Answer-推荐答案


只需向请求中添加另一个名称不同的图像。

 NSMutableData *body = [NSMutableData data];
   [body appendData:[[NSString stringWithFormat"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
   [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
   [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
   [body appendData:[NSData dataWithData:imageData]];
   [body appendData:[[NSString stringWithFormat:@"\r\n",boundary]  dataUsingEncoding:NSUTF8StringEncoding]];//MOD HERE
    //and add

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile2\"; filename=\"ipodfile2.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

在添加两个文件之前不要关闭正文--%--\r\n

[编辑]这就是我的做法:

if([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        NSData *data = [NSData dataWithContentsOfFile:path];
        //NSLog(@"image present:%d",[data length]);

        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Disposition: attachment; name=\"file1\"; filename=\"image.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        //[body appendData:[self encryptThisData:data]];
        [body appendData:data];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    if([[NSFileManager defaultManager] fileExistsAtPath:videoPath])
    {
        NSString *content = [NSString stringWithContentsOfFile:videoPath encoding:NSUTF8StringEncoding error:nil];
        NSURL *pathURL = [NSURL URLWithString:content];
        ////NSLog(@"cur url is:%@",pathURL);
        NSData *data = [NSData dataWithContentsOfURL:pathURL];
        //NSLog(@"data len is:%d",[data length]);

        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Disposition: attachment; name=\"file3\"; filename=\"video.mov\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:data];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }
// close form   
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // set request body 
    [request setHTTPBody:body];

关于ios - 将两张图片上传到 WebServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15660360/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap