duanfa0072 2017-03-06 12:05
浏览 78

Alamofire花费了太多时间上传6张图片,其中一些参数几乎总计大约2 mb

PHP side code:

<?php 

 include('dbConnect.php');
 if($_SERVER['REQUEST_METHOD']=='POST'){

 //Getting values
 $customer_email = $_POST['email'];
 $age = $_POST['age'];
 $city = $_POST['city'];
 $gender = $_POST['gender'];
 $issue_title = $_POST['issue_title'];
 $issue_desc = $_POST['issue'];
 $relation = $_POST['relation'];
 $image1 = $_POST['image1'];
 $image2 = $_POST['image2'];
 $image3 = $_POST['image3'];
 $image4 = $_POST['image4'];
 $image5 = $_POST['image5'];
 $today_date = date('Y-m-d H:i:s');
 $case_id = rand(1,100000);
 $path1 = null;
 $path2 = null;
 $path3 = null;
 $path4 = null;
 $path5 = null;
 $path6 = null;

function checkfive()
{
    global $image5,$path5;

    if($image5=="No Image")
    {
        $id5 = "default";
        $path5 = "images/$id5.jpg";
    }
    else
    {
        $id5 = rand(1,100000);
        $path5 = "images/$id5.jpg";
        file_put_contents($path5,base64_decode($image5));
    }
}

function checkfourth()
{
    global $image4,$path4;

    if($image4=="No Image")
    {
        $id4 = "default";
        $path4 = "images/$id4.jpg";
        checkfive();
    }
    else
    {
        $id4 = rand(1,100000);
        $path4 = "images/$id4.jpg";
        file_put_contents($path4,base64_decode($image4));
        checkfive();
    }
}

function checkthird()
{
    global $image3,$path3;

    if($image3=="No Image")
    {
        $id3 = "default";
        $path3 = "images/$id3.jpg";
        checkfourth();
    }
    else
    {
        $id3 = rand(1,100000);
        $path3 = "images/$id3.jpg";
        file_put_contents($path3,base64_decode($image3));
        checkfourth();
    }
}

function checksecond()
{
    global $image2,$path2;

    if($image2=="No Image")
    {
        $id2 = "default";
        $path2 = "images/$id2.jpg";
        checkthird();
    }
    else
    {
        $id2 = rand(1,100000);
        $path2 = "images/$id2.jpg";
        file_put_contents($path2,base64_decode($image2));
        checkthird();
    }
}

if($image1=="No Image")
{
    global $path1;

    $id1 = "default";   
    $path1 = "images/$id1.jpg";
    checksecond();

}
else
{
    global $path1;
    $id1 = rand(1,100000);
    $path1 = "images/$id1.jpg";
    file_put_contents($path1,base64_decode($image1));
    checksecond();
}








 $pre_sql = "SELECT * FROM customer where email='$customer_email'";
 $pre_result=mysqli_query($con,$pre_sql);

 while($row = mysqli_fetch_assoc($pre_result)){

    $customer_id = $row['customer_id'];
    $name = $row['name'];
  }

/*My Code*/
 if($issue_title=="Skin")
 {
    global $path6;
    $question1 = $_POST['question1'];
    $question2 = $_POST['question2'];
    $question3 = $_POST['question3'];
    $prescription_image = $_POST['prescription_image'];

    if($prescription_image=="No Image")
    {
        $id6 = "default";
        $path6 = "images/$id6.jpg";
    }
    else
    {
        $id6 = rand(1,100000);
        $path6 = "images/$id6.jpg";
        file_put_contents($path6,base64_decode($prescription_image));
    }

    $sql = "INSERT INTO cases (customer_id,name,age,city,gender,issue_title,relation,image1,image2,image3,image4,image5,question1,question2,question3,prescription_image,issue_desc,case_id,date) VALUES ('$customer_id','$name','$age','$city','$gender','$issue_title','$relation','$path1','$path2','$path3','$path4','$path5','$question1','$question2','$question3','$path6','$issue_desc','$case_id','$today_date')";

 }
 else{
 /*Code's End*/

 $sql = "INSERT INTO cases (customer_id,name,age,city,gender,issue_title,relation,image1,image2,image3,image4,image5,issue_desc,case_id,date) VALUES ('$customer_id','$name','$age','$city','$gender','$issue_title','$relation','$path1','$path2','$path3','$path4','$path5','$issue_desc','$case_id','$today_date')";
 }
 $result=$con->query($sql);


 //Executing query to database
if($result){


    echo 'Order  Added Successfully@' . $case_id;
}else{
    echo 'Could Not Add Order@00';
}



 //Closing the database 
 mysqli_close($con);
 }
 ?>

Swift 3 -> Alamofire code:

func globalFuncUploadFile(withParams: [String:String], forURL: String, imageData: [String:Data], completionHandler: @escaping (JSON?, NSError?) -> ()) {
    // To create a Semaphore to Sync this asyc method
    print(forURL)
    let semaphore       =   DispatchSemaphore(value: 0)
    Alamofire.upload(multipartFormData: { (MultipartFormData) in
        print("



File is appending with URL")

        for (key,data) in imageData{
            print("\(key) is appending")
            MultipartFormData.append(data.base64EncodedData(), withName: key, fileName: key, mimeType: "\(key)/JPEG)")
        }
        for (key,value) in withParams{
            print("\(key) is appending")
            MultipartFormData.append(value.data(using: .utf8)!, withName: key)
        }
        print("of size \(MultipartFormData.contentLength)")
        }, to: forURL,
           encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (Progress) in
                    print("Upload Progress: \(Progress.fractionCompleted)\t\(Progress.completedUnitCount)/\(Progress.totalUnitCount)")
                })

                upload.responseString { (value) in
                    //Show Alert in UI
                    switch value.result {
                    case .success(let valueJson):
                    print("Data uploaded \(valueJson)")
                        completionHandler(JSON(valueJson), nil)
                        break

                    case .failure(let internalError):
                        print("Data not uploaded -> \(internalError)")
                        completionHandler(nil, internalError as NSError?)
                        break
                    }
                }
              break
            case .failure(let encodingError):
                //Show Alert in UI
                print("Data not uploaded -> \(encodingError)")
                completionHandler(nil, encodingError as NSError?)
                break
            }


            //Awaking Semaphore that session is complete
            semaphore.signal()
        }
    )
    // Waiting for the semaphore signal
    _ = semaphore.wait(timeout: .distantFuture)
}

If by any chance taking almost a minute, Images would uploaded to server by getting successful message from php code, then the images will not saved to database and generates this error:

PHP Notice:  Undefined index: image1 in address.php on line 14
PHP Notice:  Undefined index: image2 in address.php on line 15
PHP Notice:  Undefined index: image3 in address.php on line 16
PHP Notice:  Undefined index: image4 in address.php on line 17
PHP Notice:  Undefined index: image5 in address.php on line 18
PHP Notice:  Undefined index: prescription_image in address.php on line 143

But most of the times, say 99 times out of 100; The time out error is generates in swift by alamofire (Swift Error Log with uploading process):

of size 2658665
Upload Progress: 0.0123166068590059 32768/2660473
Upload Progress: 0.0246332137180118 65536/2660473
Upload Progress: 0.0369498205770177 98304/2660473
Upload Progress: 0.0492664274360236 131072/2660473
Upload Progress: 0.0615830342950295 163840/2660473
Upload Progress: 0.0738996411540354 196608/2660473
Upload Progress: 0.0862162480130413 229376/2660473
Upload Progress: 0.0985328548720472 262144/2660473
Upload Progress: 0.110849461731053  294912/2660473
Upload Progress: 0.123166068590059  327680/2660473
Upload Progress: 0.135482675449065  360448/2660473
Upload Progress: 0.147799282308071  393216/2660473
Upload Progress: 0.160115889167077  425984/2660473
Upload Progress: 0.172432496026083  458752/2660473
Upload Progress: 0.184749102885088  491520/2660473
Upload Progress: 0.197065709744094  524288/2660473
Upload Progress: 0.2093823166031    557056/2660473
Upload Progress: 0.221698923462106  589824/2660473
Upload Progress: 0.234015530321112  622592/2660473
Upload Progress: 0.246332137180118  655360/2660473
Upload Progress: 0.258648744039124  688128/2660473
Upload Progress: 0.27096535089813   720896/2660473
Upload Progress: 0.283281957757136  753664/2660473
Upload Progress: 0.295598564616142  786432/2660473
Upload Progress: 0.307915171475147  819200/2660473
Upload Progress: 0.320231778334153  851968/2660473
Upload Progress: 0.332548385193159  884736/2660473
Upload Progress: 0.344864992052165  917504/2660473
Upload Progress: 0.357181598911171  950272/2660473
Upload Progress: 0.369498205770177  983040/2660473
Upload Progress: 0.381814812629183  1015808/2660473
Data not uploaded -> Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x12ed8a7d0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://***.php, NSErrorFailingURLKey=http://***.php, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}

Error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x12ed8a7d0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://***.php, NSErrorFailingURLKey=http://***.php, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}

Hmm some extra information: The whole thing is working for both WIFI and for Network when I was testing in mobile with SIM but when SIM got ejected by the owner; I left up with only WIFI, and face this thing.

There is gap of a day between SIM and no SIM scenario as I'm on leave. In between there have some changes on php side which is hard to mention by me. Thanks in advance for your cooperation.

  • 写回答

1条回答 默认 最新

  • douru5373 2017-03-08 06:02
    关注

    Sad but no help in 72 hours and as there is saying "Who doesn't got help from anywhere, eventually able to help himself".

    The problem is in data format. The php side guy changed the method of getting image into base64String which is previously in data format and on swift side I'm still sending image in data format. When alamofire is trying to handshake with php, php got confused by data format and takes long time to reply back.

    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败