我想使用javascript重命名在客户端使用相机拍摄的图像。我知道由于安全原因,这是不可能的。
我已经在服务器端做过了,但我需要在上传到服务器之前重命名我的图像。我可以复制图像然后重命名吗?如果可以的话,我该怎么做?
这是我的javascript文件
var oFile = new File([""],"");
var clonedImage = new Image();
//cloning function
function cloneImage ()
{
alert("cloning1");
clonedImage.name = oFile.name;
document.getElementById("image_file").appendChild( clonedImage);
alert("cloning2");
alert("success" +clonedImage.name );
}
function fileSelected()
{
// get selected file element
oFile = document.getElementById('image_file').files[0];
// filter for image files
var rFilter = /^(image\/bmp|image\/gif|image\/jpeg|image\/png|image\/tiff)$/i;
if (! rFilter.test(oFile.type)) {
alert("Not a proper file format");
return;
}
cloneImage();
alert("cloning");
var oReader = new FileReader();
oReader.readAsDataURL(oFile);
alert("success = "+oFile.name);
alert("success = "+oFile.type);
}
function startUploading()
{
var formdata = new FormData(document.getElementById("upload_form"));
var imageName = oFile.name;
formdata.append("image1",oFile);
var request = new XMLHttpRequest();
request.open('POST', 'http://10.182.xx.xx/upload.php',true);
alert("start uploading");
alert("success ="+oFile.name);
request.send(formdata);
alert("finished");
}
这是我的html文件
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<link href="file:///android_asset/main.css" rel="stylesheet" type="text/css" />
<script src="file:///android_asset/script.js" type ="text/javascript"> </script>
</head>
<body>
<form id="upload_form" enctype="multipart/form-data" method="POST" action="upload.php">
<div>
<div><label for="image_file">Please select image file</label></div>
<div><input type="file" name="image_file" id="image_file" onchange="fileSelected()" /></div>
</div>
<div>
<input type="button" name="image1" id="image1" value="Upload" onclick="startUploading()"/>
</div>
</form>
</body>
</html>