菜鸟教程小白 发表于 2022-12-12 09:54:43

ios - 将两张图片上传到 WebServer


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

<pre><code>NSData *imageData = UIImagePNGRepresentation(imageToSend);
// setting up the URL to post to
NSString *urlString = @&#34;http://www.myweb.com.br/_resources/testeDir.php&#34;;

// setting up the request object now
NSMutableURLRequest *request = [ init];
];
;

NSString *boundary = @&#34;14737809831466499882746641449&#34;;
NSString *contentType = ;
;

/*
now lets create the body of the post
*/
NSMutableData *body = ;
dataUsingEncoding:NSUTF8StringEncoding]];
];
];
];
dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
;

//Create the connection with the string URL and kick it off
NSURLConnection *urlConnection = ;
;
</code></pre>

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

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

<pre><code>AFHTTPClient *httpClient = [ initWithBaseURL:urlString];
NSMutableURLRequest *requestHTTP = [httpClient multipartFormRequestWithMethod:@&#34;POST&#34; path:@&#34;/&#34; parameters:nil constructingBodyWithBlock: ^(id &lt;AFMultipartFormData&gt;formData) {
    ;
}];

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

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


}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@&#34;Upload succes&#34;);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@&#34;Upload failed&#34;);
}];
;
</code></pre>

<p>这是我的 php 代码:</p>

<pre><code>&lt;?php
    $currentDirectory = getcwd();
    $uploaddir = $currentDirectory.&#34;/4/HD/&#34;;
    $uploaddir2 = $currentDirectory.&#34;/4/thumb/&#34;;
    $file = basename($_FILES[&#39;userfile&#39;][&#39;name&#39;]);
    $uploadfile = $uploaddir . $file;
    $file2 = basename($_FILES[&#39;userfile2&#39;][&#39;name2&#39;]);
    $uploadfile2 = $uploaddir2 . $file2;

if (move_uploaded_file($_FILES[&#39;userfile&#39;][&#39;tmp_name&#39;], $uploadfile)) {
      echo $uploaddir.&#34;{$file}&#34;;
      }

if (move_uploaded_file($_FILES[&#39;userfile2&#39;][&#39;tmp_name&#39;], $uploadfile2)) {
      echo $uploaddir2.&#34;{$file2}&#34;;
      }
?&gt;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>只需向请求中添加另一个名称不同的图像。</p>

<pre><code> NSMutableData *body = ;
    dataUsingEncoding:NSUTF8StringEncoding]];
   ];
   ];
   ];
   dataUsingEncoding:NSUTF8StringEncoding]];//MOD HERE
    //and add

    dataUsingEncoding:NSUTF8StringEncoding]];
    ];
    ];
    ];
    dataUsingEncoding:NSUTF8StringEncoding]];
</code></pre>

<p>在添加两个文件之前不要关闭正文<em>--%--\r\n</em></p>

<p>[编辑]这就是我的做法:</p>

<pre><code>if([ fileExistsAtPath:path])
    {
      NSData *data = ;
      //NSLog(@&#34;image present:%d&#34;,);

       dataUsingEncoding:NSUTF8StringEncoding]];
      ];
      ];
      //];
      ;
      ];
    }
    if([ fileExistsAtPath:videoPath])
    {
      NSString *content = ;
      NSURL *pathURL = ;
      ////NSLog(@&#34;cur url is:%@&#34;,pathURL);
      NSData *data = ;
      //NSLog(@&#34;data len is:%d&#34;,);

       dataUsingEncoding:NSUTF8StringEncoding]];
      ];
      ];
      ;
      ];
    }
// close form   
    dataUsingEncoding:NSUTF8StringEncoding]];

    // set request body
    ;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将两张图片上传到 WebServer,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15660360/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15660360/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将两张图片上传到 WebServer