简体   繁体   English

发送到服务器时不完整的Base64

[英]Incomplete Base64 when sending to server

I am trying to send an image to server, but the image should be in Base64 format. 我正在尝试将图像发送到服务器,但是该图像应为Base64格式。 I am using this function to send it: 我正在使用此功能发送它:

func upload_signature_staff(ticket: NSString){
        let defaults = NSUserDefaults.standardUserDefaults()
        let stringOne = defaults.stringForKey(defaultsKeys.staff_id)
        let stringtwo = defaults.stringForKey(defaultsKeys.mst_customer)
        let sig = defaults.stringForKey("staff_signature")
        let request = NSMutableURLRequest(URL: NSURL(string: "http://xxxxxxxxxxxxx/upload.php")!)
        request.HTTPMethod = "POST"
        let postString = "action=add_signature&mst_customer=\((stringtwo!))&ticket=\((ticket))&signature=\((sig!))&current_user=\((stringOne!))&item_type=10"
        print(postString)
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
            guard error == nil && data != nil else {                                                          // check for fundamental networking error
                print("error=\(error)")
                return
            }

            if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            }

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()
        //self.performSegueWithIdentifier("goto_main2", sender: self)
    }

The sig variable holds the Base64 string, it is being printed in my console so I can verify that the string is correct. sig变量保存Base64字符串,它正在控制台中打印,因此我可以验证该字符串是否正确。 I am also printing the postString and upon inspection it is also correct that the signature is matching the Base64 String. 我也在打印postString,经检查,签名与Base64字符串匹配也是正确的。 But when I open phpmyadmin, I see the field of my image with incomplete Base64 string, maybe 1/4 is just there. 但是,当我打开phpmyadmin时,我看到的图像字段包含不完整的Base64字符串,也许只有1/4在那里。

Here's my php code, in case you want to see it: 这是我的php代码,以防您想要看到它:

<?php
require_once("../c.php");

$action = trim($_POST['action']);

if($action == "add_signature"){
    $mst_customer = trim($_POST['mst_customer']);
    $ticket = trim($_POST['ticket']);
    $signature = trim($_POST['signature']);
    $current_user = trim($_POST['current_user']);
    $item_type = trim($_POST['item_type']);
    $inputtime = time();

    $sql = "INSERT INTO ticket_items SET mst_customer = '$mst_customer', ticket = '$ticket', ticket_item_type = '$item_type', details = '$signature', addedby = '$current_user', lastupdate = '$inputtime' ";

    mysql_query($sql) or die(mysql_error());
}

?>

I think this was solved in the comments but here's a recap: 我认为这已在评论中解决,但这里有个回顾:

Inserting base64 strings that were too long (variable length?) in a varchar(255) field resulted in missing data. 在varchar(255)字段中插入太长(可变长度?)的base64字符串会导致数据丢失。 As far as I can tell, increasing the size of the field solved the immediate problem. 据我所知,增加领域的规模解决了眼前的问题。 I use the term "immediate" because, as @YvesLeBorg pointed out in the comments, this is bound to fail at some point without input size restrictions on the backend. 我使用“立即”一词是因为,正如@YvesLeBorg在评论中指出的那样,这一定会在没有后端输入大小限制的情况下失败。

Additionally, I couldn't ignore the fact that the PHP/SQL code was wide open to injections. 另外,我不能忽略这样一个事实,即PHP / SQL代码对注入很开放。

Passing $mst_customer = trim($_POST['mst_customer']); 传递$mst_customer = trim($_POST['mst_customer']); on to "INSERT INTO ticket_items SET mst_customer = '$mst_customer' and then executing via mysql_query($sql) or die(mysql_error()); is dangerous! 继续执行"INSERT INTO ticket_items SET mst_customer = '$mst_customer' ,然后通过mysql_query($sql) or die(mysql_error());是危险的!

Anybody could write anything in the $_POST parameter and SQL would happily accept it. 任何人都可以在$_POST参数中编写任何内容,SQL会很乐意接受它。 Prepared statements, PDO, input sanitization etc. are there for a reason. 有准备的语句,PDO,输入清理等是有原因的。

Finally, there was an issue concerning vanishing + signs in the base64 data. 最后,在base64数据中存在一个有关消失+符号的问题。 This was the result of missing url encoding of the post data. 这是由于缺少发布数据的url编码的结果。

I think that sums it up. 我认为可以总结一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM