重命名BLOB form.append [英] Rename BLOB form.append

查看:52
本文介绍了重命名BLOB form.append的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我很困惑,我已经能够成功将调整大小的图像/blob上传到服务器文件夹.问题是图像/blob上传始终被称为blob.是否可以在客户端更改名称,还是应该在服务器PHP上更改名称?.
如果可以,请给我一个例子,这里是我用来与之通信的2个脚本

Ok I am stumped, I have been able to successfully upload resized image/blob to server folder. The problem is that the image/blob upload is always called blob. Is there a way to change name on client side or should i do it on server PHP side?.
And if so can you please give me an example here is the 2 scripts I am using to communicate with

客户端调整大小

<script>
function handleFiles(){
var dataurl = null;
var filesToUpload = document.getElementById('input').files;
var file = filesToUpload[0];

// Create an image
var img = document.createElement("img");
// Create a file reader
var reader = new FileReader();
// Set the image once loaded into file reader
reader.onload = function(e)
{
    img.src = e.target.result;

    img.onload = function () {
        var canvas = document.createElement("canvas");
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0);

        var MAX_WIDTH = 200;
        var MAX_HEIGHT = 400;
        var width = img.width;
        var height = img.height;

        if (width > height) {
          if (width > MAX_WIDTH) {
            height *= MAX_WIDTH / width;
            width = MAX_WIDTH;
          }
        } else {
          if (height > MAX_HEIGHT) {
            width *= MAX_HEIGHT / height;
            height = MAX_HEIGHT;
          }
        }
        canvas.width = width;
        canvas.height = height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, width, height);
        dataurl = canvas.toDataURL("image/jpeg",.2);
        var blobBin = atob(dataurl.split(',')[1]);
        var array = [];
        for(var i = 0; i < blobBin.length; i++) {
        array.push(blobBin.charCodeAt(i));
        }
       var files = new Blob([new Uint8Array(array)], {type: 'image/jpg', name: ""});
       var filename = getFileName()



        // Post the data
        var fd = new FormData();
        fd.append("image",files);
        $.ajax({
            url: 'http:///www.i-audit-jci.com/upload.php',
            data: fd,
            cache: false,
            contentType: false,
            processData: false,
            type: 'POST',
            success: function(data){
                $('#form_input')[0].reset();
                location.reload();
            }
        });
    } // img.onload
}
// Load files into file reader
reader.readAsDataURL(file);
}
</script>

服务器PHP

<?php
$upload_image = $_FILES["image"][ "name" ];
$a = ('" alt="" />');
$folder = "images/";
move_uploaded_file($_FILES["image"]["tmp_name"], "$folder".$_FILES["image"]["name"]);;
$file = 'images/'.$_FILES["image"]["name"];
$uploadimage = $folder.$_FILES["image"]["name"];
$newname = $_FILES["image"]["name"];
$msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
$a = ('" alt="" />');
$image = $_FILES['image']['tmp_name'];
$img = file_get_contents($image);
$con = mysqli_connect('mysql***','***','***','***') or die('Unable To connect');
$sql = ("INSERT into links (hyper_links) VALUES('<img src=\"\https://www.i-audit-jci.com/images/".$_FILES['image']['name']."$a')");

$stmt = mysqli_prepare($con,$sql);

mysqli_stmt_bind_param($stmt, "s",$img);
mysqli_stmt_execute($stmt);

$check = mysqli_stmt_affected_rows($stmt);
if($check==1){
    $msg = 'Successfullly UPloaded';
}else{
    $msg = 'Could not upload';
}
mysqli_close($con);
}
?>
<?php
echo $msg;
?>

推荐答案

您可以在的第三个参数上设置传递给 FormData File 对象的name属性.> FormData.append()函数

You can set the name property of a File object passed to FormData at third parameter of FormData.append() function

var blob = new Blob([123], {
  type: "text/plain"
});
var data = new FormData();
// set `blob` name to `"file.txt"`
data.append("file", blob, "file.txt");

console.log(data.get("file"), data.get("file").name);

这篇关于重命名BLOB form.append的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆