duanchouyi6730 2018-08-16 16:45
浏览 28
已采纳

使用PHP或Javascript重命名本地磁盘上的文件

I have a scenario in which user is prompted for selecting a file from local disk (an image). Upon selection, this image is shown in browser (without uploading). After seeing the image user inputs a number in CN No field. Upon submit I want to rename the file on local disk with the number input by user. Is there some way to do this in code using PHP or Javascript?

<html>
<head>
<script type='text/javascript'>
function preview_image(event) 
{
 var reader = new FileReader();
 reader.onload = function()
 {
  var output = document.getElementById('output_image');
  output.src = reader.result;
  }
  reader.readAsDataURL(event.target.files[0]);
  }
</script>
</head>
<body>
<form>
 Select File: <input type="file" accept="image/*"     onchange="preview_image(event)"> <br>
 Enter CN No. <input type="number" id="cnno" name="cnno"> <br>
 <input id="sbt" type="submit" name="submit" value="Submit" accesskey="u">     <br>
</form>

 <img id="output_image" style="width: 400px"/>

</body>
</html>
  • 写回答

2条回答 默认 最新

  • douzhi7661 2018-08-16 17:17
    关注

    You could do this, presuming downloading the file is ok.

    <html>
    <head>
      <script type='text/javascript'>
      var img;
    
      function preview_image(elm) {
       var reader = new FileReader();
       reader.onload = function(event) {
         img = event.target.result
         document.getElementById("image_preview").src = img;
       };
       reader.readAsDataURL(elm.files[0]);
      }
    
      function download() {
        var element = document.createElement('a');
        element.setAttribute('href', img)
        element.setAttribute('download', document.getElementById("cnno").value+'.png');
    
        element.style.display = 'none';
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
      }
      </script>
    </head>
    <body>
    
    Select File: <input type="file" accept="image/*" onchange="preview_image(this)"> <br>
    Enter CN No. <input type="number" id="cnno" name="cnno"> <br>
    <input id="sbt" type="submit" name="submit" value="Submit" accesskey="u" onclick="download()"><br>
    
    <img id="image_preview" style="width: 400px"/>
    
    </body>
    </html>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记