【问题标题】:Show file link after uploading上传后显示文件链接
【发布时间】:2016-06-25 05:51:57
【问题描述】:

我已经从 Link 下载了一个带有 Ajax 的图片上传脚本

代码是:

index.php

<script language="javascript" type="text/javascript">
<!--
function startUpload(){
      document.getElementById('f1_upload_process').style.visibility = 'visible';
      document.getElementById('f1_upload_form').style.visibility = 'hidden';
      return true;
}

function stopUpload(success, str){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      document.getElementById('f1_upload_form').innerHTML = result + str+'<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
      document.getElementById('f1_upload_form').style.visibility = 'visible';      
      return true;   
}
//-->
</script>   
</head>

<body>
       <div id="container">
            <div id="header"><div id="header_left"></div>
            <div id="header_main">Max's AJAX File Uploader</div><div id="header_right"></div></div>
            <div id="content">
                <form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
                     <p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
                     <p id="f1_upload_form" align="center"><br/>
                         <label>File:  
                              <input name="myfile" type="file" size="30" />
                         </label>
                         <label>
                             <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                         </label>
                     </p>

                     <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
                 </form>
             </div>
             <div id="footer"><a href="http://www.ajaxf1.com" target="_blank">Powered by AJAX F1</a></div>
         </div>

</body>   

Uploader.php

<?php
       // Edit upload location here
       $destination_path = getcwd().DIRECTORY_SEPARATOR;

       $result = 0;

       $target_path = $destination_path . basename( $_FILES['myfile']['name']);

       if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
          $result = 1;
         // echo $target_path;
       }

       sleep(1);
    ?>

    <script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result.",".$target_path; ?>);</script>   

我想显示上传图片的网址。以便它可以立即使用。 我对ajax有点了解。我试着写了两三个代码,但它不起作用。

任何人都可以帮助显示文件 url。 提前致谢。

【问题讨论】:

    标签: php jquery ajax file-upload


    【解决方案1】:

    希望这会有所帮助... index.php

        <html>
    <head>
            <script src="js/jquery-2.1.4.min.js"></script>
            <script src="js/bootstrap.min.js"></script>
    </head>
    
    <body>
    <script language="javascript" type="text/javascript">
    <!--
    function startUpload(){
    
          document.getElementById('f1_upload_process').style.visibility = 'visible';
          document.getElementById('f1_upload_form').style.visibility = 'hidden';
          return true;
    }
    
    function stopUpload(success, str, filename){
          var result = '';
          var file_link = '';
    
            var location = window.location.href;
            var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
    
          if (success == 1){
             result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
             file_link = "<br/><a href='"+directoryPath+"/"+filename +"'>"+filename+"</a><br/>";
          }
          else {
             result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
          }
          document.getElementById('f1_upload_process').style.visibility = 'hidden';
          document.getElementById('f1_upload_form').innerHTML = result + file_link +'<br/><br/><label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
          document.getElementById('f1_upload_form').style.visibility = 'visible';      
          return true;   
    }
    //-->
    </script>   
    </head>
    
    <body>
           <div id="container">
                <div id="header"><div id="header_left"></div>
                <div id="header_main">Max's AJAX File Uploader</div><div id="header_right"></div></div>
                <div id="content">
                    <form action="ajax-uploader.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
                         <p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
                         <p id="f1_upload_form" align="center"><br/>
                             <label>File:  
                                  <input name="myfile" type="file" size="30" />
                             </label>
                             <label>
                                 <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                             </label>
                         </p>
    
                         <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
                     </form>
                 </div>
                 <div id="footer"><a href="http://www.ajaxf1.com" target="_blank">Powered by AJAX F1</a></div>
             </div>
    
    </body>   
        </body>
    </html>
    

    ajax-uploader.php

        <?php
       // Edit upload location here
       $destination_path = getcwd().DIRECTORY_SEPARATOR;
    
       $result = 0;
    
       $target_path = $destination_path  . basename( $_FILES['myfile']['name']);
       $actual_name = basename( $_FILES['myfile']['name']);
    
       if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
          $result = 1;
         // echo $target_path;
       }
    
       sleep(1);
    ?>
    
    <script language="javascript" type="text/javascript">window.top.stopUpload(<?php echo "$result, '$target_path','$actual_name' "; ?>);</script>  
    

    【讨论】:

      【解决方案2】:

      Uploader.php 更改为upload.php

      sleep后添加php代码如下..

      sleep(1);
         $target_path = str_replace('\\', '/', $target_path);
         $spath = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI'])."/".$_FILES['myfile']['name'];
      
      ?>
      
      <script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo "'".$result."','".$target_path.',img url :'.$spath."'";?>);</script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-23
        • 1970-01-01
        相关资源
        最近更新 更多