使用文件 phonegap 插件“image%A3456"上传的图片名称 [英] Name of picture uploaded with file phonegap plugin "image%A3456"

查看:36
本文介绍了使用文件 phonegap 插件“image%A3456"上传的图片名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 FileTransfert 插件 phonegap 将图片从手机上传到服务器.代码工作正常,但图片上传的名称是

I use a FileTransfert plugin phonegap to upload picture from phone to server. The code work right but the name of picture upload is

图片%A4444

但是我想保存同名的图片,然后在手机上显示.

But I want to save the picture with the same name does it have on phone to display then.

我应该修改什么代码?

谢谢

函数js:

    function uploadImage() {
    document.getElementById('picture_msg').innerHTML = "";
    // Get URI of picture to upload
    navigator.camera.getPicture(
        //function(uri) {
        function(imageURI) {
            try {
                // Pick image from div
                 var img = document.getElementById('pimage');
                img.style.visibility = "visible";
                img.style.display = "block";
                //var imageURI = uri;
                if (!imageURI || (img.style.display == "none")) {
                    document.getElementById('picture_msg').innerHTML = "Tap on picture to select image from gallery.";
                    return;
                }
                // Verify server has been entered
                server = document.getElementById('server').value;
                console.log("Server "+server);
                if (server) {
                    // Specify transfer options
                    var options = new FileUploadOptions();
                    options.fileKey="file";
                    options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+'.png';
                    options.mimeType="image/png";
                    //options.mimeType=mimeType;
                    options.chunkedMode = false;
                    options.headers = {
                         Connection: "close"
                    };

                    // Transfer picture to server
                    var ft = new FileTransfer();
                    ft.upload(imageURI, server, function(r) {
                         window.FilePath.resolveNativePath(imageURI, function(result) {
                        document.getElementById('picture_msg').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";
                             /* window.FilePath.resolveNativePath(imageURI, function(result) {
                              document.getElementById('picture_msg').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded."; */
                             // onSuccess code
                              imageURI =  result;
                        img.src = imageURI;
                        img.width = 100;
                        img.height = 100;
                          });            
                        //alert(uri);
                    },
                function(error) {
                        document.getElementById('picture_msg').innerHTML = "Upload failed: Code = "+error.code;
                    }, options);
                }
                else {
                    document.getElementById('picture_msg').innerHTML = "Server Not Found";
                }
            }
            catch(exce) {
                alert(exce);
            }
        },
        function(e) {
            console.log("Error getting picture: " + e);
            document.getElementById('picture_msg').innerHTML = "No Image Found";
        },
        {
            quality: 50,
            destinationType: navigator.camera.DestinationType.FILE_URI,
            sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
        }
    );
}

上传.php

    <?php
   // Directory where uploaded images are saved
$dirname = "phonegapserver/uploads/"; 
// If uploading file
if ($_FILES) {
    print_r($_FILES);
    mkdir ($dirname, 0777, true); 
    move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
}
// If retrieving an image
else if (isset($_GET['image'])) {
    $file = $dirname."/".$_GET['image'];
    // Specify as jpeg
    header('Content-type: image/png');

    // Resize image for mobile
    list($width, $height) = getimagesize($file); 
    $newWidth = 120.0; 
    $size = $newWidth / $width;
    $newHeight = $height * $size; 
    $resizedImage = imagecreatetruecolor($newWidth, $newHeight); 
    $image = imagecreatefromjpeg($file); 
    imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    imagejpeg($resizedImage, null, 80); 
}
// If displaying images
else {
    $baseURI = "http://".$_SERVER['localhost'].':'.$_SERVER['8080'].$_SERVER['REQUEST_URI'];
    $images = scandir($dirname);
    $ignore = Array(".", "..");
    if ($images) {
        foreach($images as $curimg){ 
            if (!in_array($curimg, $ignore)) {
                echo "Image: ".$curimg."<br>";
                echo "<img src='".$baseURI."?image=".$curimg."&rnd=".uniqid()."'><br>"; 
            }
        }
    }
    else {
        echo "No images on server";
    }
}


?>

推荐答案

选项对象中的文件名设置似乎是问题所在.设置正确的文件名可以解决问题.

Filename setting in options object seems to be the issue. Setting proper filename resolves the issue.

这篇关于使用文件 phonegap 插件“image%A3456"上传的图片名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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