简体   繁体   English

通过 AJAX POST 发送 URI 数据

[英]Sending URI data via AJAX POST

I got some trouble sending long text strings via html forms.我在通过 html 表单发送长文本字符串时遇到了一些麻烦。

I want to send image data uri to a php page there can handle the data and save it in MySQL.我想将图像数据 uri 发送到一个 php 页面,那里可以处理数据并将其保存在 MySQL 中。

Image data example:图像数据示例:

data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfO..数据:图像/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfO ..

The data is coming from a clipboard paste function I have on the page.数据来自我在页面上的剪贴板粘贴功能。

I have no problem in saving the data on the php page, but getting the data to the page makes trouble.我在php页面保存数据没有问题,但是获取数据到页面就麻烦了。

The script below is the one i try use to send the data from the client page:下面的脚本是我尝试用来从客户端页面发送数据的脚本:

formData = new FormData();
formData.append('imagedata','data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfO..');
$.ajax({
    url: "test.php?reportid=1", 
    type: "POST", 
    cache: false,
    contentType: false,
    processData: false,
    data: formData
}).done(function(e){
    alert(e);
});

It works fine if the image size is small, but if i got an image above 250KB, it loose data during the post.如果图像尺寸很小,它可以正常工作,但是如果我的图像大于 250KB,它会在发布过程中丢失数据。

Maybe someone has a better way to post the data to the server?也许有人有更好的方法将数据发布到服务器?

I found an alternative to transfer the data.我找到了传输数据的替代方法。

Using: https://stackoverflow.com/a/5100158/3176569 for converting base64 code to blob使用: https : //stackoverflow.com/a/5100158/3176569将 base64 代码转换为 blob

function dataURItoBlob(dataURI) {
  // convert base64/URLEncoded data component to raw binary data held in a string
  var byteString;

  if (dataURI.split(',')[0].indexOf('base64') >= 0)
    byteString = atob(dataURI.split(',')[1]);
  else
    byteString = unescape(dataURI.split(',')[1]);

  // separate out the mime component
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

  // write the bytes of the string to a typed array
  var ia = new Uint8Array(byteString.length);

  for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
  }

  return new Blob([ia], {
    type: mimeString
  });
}

I change the code to this:我把代码改成这样:

formData = new FormData();
var blob = dataURItoBlob('data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfO..');

formData.append('imagedata', blob, 'temp.png');

var request = new XMLHttpRequest();
request.open("POST", "test.php?reportid=1");
request.send(formData); 

And then on the PHP page i receive data via $_FILES[];然后在 PHP 页面上,我通过 $_FILES[]; 接收数据;

$file = $_FILES['imagedata'];
$filetype = $file['type'];
$blob = file_get_contents($file['tmp_name']);

After that, can I insert it into the MySQL database.之后,我可以将它插入到MySQL数据库中吗?

Well, thanks anyway for the replies and suggestions :)好吧,无论如何感谢您的回复和建议:)

尝试 JSON.parse('data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfO..');

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

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