在一个网站上,我使用JavaScript将文件从本地计算机上传到网络服务器。使用javascript更新图像src
我正在使用javascript将文件上传到服务器并更新表单文本字段。 在同一个文档中,还有一个图像,除了表单之外,我想用刚刚上传的图像文件更新图像。
这是我的javascript来实现两件事:
更改格式的文本字段:
opener.document.form1.logo_material.value="?php echo $prefijo.$str;?>";
(在
<
之前?php
省略由于编辑限制) 这部分工作正常。只是上传的图片更改图片:
opener.document.getElementById("materiallogo").src="..url hidden here../?php echo $prefijo.$str;?>";
(在<
之前?php
省略由于编辑限制)
这部分#2不工作。欢迎任何帮助。由于
编辑
表单按钮来调用JS功能:
<input type="button" name="button" id="button" value="Upload logo" onclick="javascript:subirimagen();" />
JavaScript函数主文档中:
<script>
function subirimagen()
{
self.name = 'opener';
remote = open('subirimagenmaterial.php','remote','width=300,height=150,location=no,scrollbars=yes, menubar=no, toolbars=no,resizable=yes,fullscreen=yes, status=yes');
remote.focus();
}
</script>
PHP管理所有文件上传和文本字段的变化和还应该管理图像更新:
subirimagenmate rial.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload Material logo</title>
</head>
<body>
<?php
if ((isset($_POST["enviado"])) && ($_POST["enviado"]=="form1")){
$length = 10;
$randomString = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$prefijo = $randomString;
$nombre_archivo = $_FILES['userfile']['name'];
$file_upload = "true";
if ($_FILES['userfile']['size']>200000){
$file_upload="false";
}
if ($file_upload == 'true'){
$str=preg_replace('/\s+/', '', $nombre_archivo);
$str =preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($str, ENT_QUOTES, 'UTF-8'));
move_uploaded_file($_FILES['userfile']['tmp_name'],"materials/".$prefijo.$str);
?>
<script>
opener.document.form1.logo_material.value="<?php echo $prefijo.$str;?>";
opener.document.getElementById("materiallogo").src="h..URL hidden here/<?php echo $prefijo.$str;?>";
self.close();
</script>
<?php
}
else
{
echo "El archivo seleccionado es superior a 200KB, inténtalo de nuevo con otro archivo de tamaño inferior a 200KB.<BR><BR><BR>";?>
<input type='submit' name='submit' value='Reintentar' onClick="window.location.reload()" />
<?php }
?>
<?php
}
else {?>
<form id="form1" name="form1" method="post" action="subirimagenmaterial.php" data-ajax="false" enctype="multipart/form-data">
<p>
<input name="userfile" type="file" />
</p>
<p>
<input type="submit" name="button" id="button" value="Subir imagen del evento" />
<input type="hidden" name="enviado" value="form1" />
</p>
</form>
<?php }?>
</body>
</html>
到底是什么揭幕战? document.getElementById(“materiallogo”)。src应该可以正常工作。 –
请将您的完整代码添加到您的问题! – mohsen
@Dontfeed代码,前缀'opener'用于引用已调用外部js文件的文件。 JS文件打开一个窗口,从计算机中选择一个文件,然后关闭时,它会更改表单文本字段的值,并且还应该更改图像 – mvasco