使用angularjs上传图像并保存在本地存储中

使用angularjs上传图像并保存在本地存储中,angularjs,local-storage,image-uploading,Angularjs,Local Storage,Image Uploading,我想在我的页面上有一个按钮,从那里我可以上传本地系统的图像,然后我想保存在我的本地存储图像 我渴望在这里学习angularjs 您希望将图像编码为base 64字符串,并将其存储在本地存储器中 有关如何将图像转换为基64字符串的示例,请参见toDataURL()返回一个字符串,然后可以使用通常在JSON对象中存储字符串的方式存储该字符串 要显示图像,请使用以下内容: <img src="data:image/jpeg;base64,blahblahblah"></img>

我想在我的页面上有一个按钮,从那里我可以上传本地系统的图像,然后我想保存在我的本地存储图像


我渴望在这里学习angularjs

您希望将图像编码为base 64字符串,并将其存储在本地存储器中

有关如何将图像转换为基64字符串的示例,请参见
toDataURL()
返回一个字符串,然后可以使用通常在JSON对象中存储字符串的方式存储该字符串

要显示图像,请使用以下内容:

<img src="data:image/jpeg;base64,blahblahblah"></img>


其中
blahblahblah
是返回的字符串。

按照下面的代码使用AngularJS上传和保存图像

创建index.php文件并初始化应用程序并创建AngularJS控制器

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular-route.min.js"></script>
        <script src="app.js"></script>
    </head>
    <body ng-app="myApp" ng-controller="myCtrl">
        <div>
            <input type="file" file-model="myFile"/>
            <button ng-click="uploadFile()">upload me</button>
        </div>
    </body>
 </html>
在此之后,创建post.php文件以将文件上载到存储器中

<?php $upload_dir = "images/"; 
if(isset($_FILES["file"]["type"]))
{ 
    $validextensions = array("jpeg", "jpg", "png", "gif");
    $temporary = explode(".", $_FILES["file"]["name"]);
    $file_extension = end($temporary);
    if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg")) && in_array($file_extension, $validextensions)) {
        if ($_FILES["file"]["error"] > 0){
            echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
        } else {
            if (file_exists($upload_dir.$_FILES["file"]["name"])) {                
                echo 'File already exist';
            } else {
                $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
                $filename = rand().$_FILES['file']['name'];
                $targetPath = $upload_dir.$filename; // Target path where file is to be stored
                move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
                echo 'success';
            }
        }
    } 
} ?>

创建图像文件夹。
希望这对你有帮助。参考:

首先,谢谢。但是通过这样做,我是否能够将此图像存储在json对象中?基本上,我该如何阅读图片的内容?@AdityaSethi已将我的答案扩展到了封面that@MatthewDaly,兄弟,如果我通过http请求将这个json字符串发送到服务器,你能帮我吗?我能把这个字符串代码再次解码成jpg、png等扩展名吗??帮助我out@Tech您可以在服务器上对其进行解码,但实际的解码方法显然取决于语言。如果你用谷歌搜索像“$LANGUAGE\u NAME decode base64”这样的东西,你应该会找到一些东西。@AdityaSethi我也有类似的要求,我是Angular Js的新手。你能不能给一些示例代码,我不想创建任何服务器,只是让用户上传图像,我想保存该图像本地或如果可能的话进一步使用。
<?php $upload_dir = "images/"; 
if(isset($_FILES["file"]["type"]))
{ 
    $validextensions = array("jpeg", "jpg", "png", "gif");
    $temporary = explode(".", $_FILES["file"]["name"]);
    $file_extension = end($temporary);
    if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg")) && in_array($file_extension, $validextensions)) {
        if ($_FILES["file"]["error"] > 0){
            echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
        } else {
            if (file_exists($upload_dir.$_FILES["file"]["name"])) {                
                echo 'File already exist';
            } else {
                $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
                $filename = rand().$_FILES['file']['name'];
                $targetPath = $upload_dir.$filename; // Target path where file is to be stored
                move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
                echo 'success';
            }
        }
    } 
} ?>