douji1999 2016-07-01 11:18
浏览 27
已采纳

显示没有页面刷新的docx

I am curious to know that is it possible to show a docx file (which is available on server) inside a div without refreshing that page (using google doc viewer API or any other possible way).

Let me clear my requirement:-

HTML+JQUERY code:

  <div class="browse-flie col-md-7">
      <div class="alert alert-danger" style = "display:none;"></div>
    <input type="file" id="uploaddocfile">
    <button name="upload" value="Upload" class="btn EditBtn uplaod_file">Upload</button>
  </div>
  <div id = "myid" style = "height:500px;width:600px;"></div>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
        $('.uplaod_file').on('click', function() {
          var file_data = $('#uploaddocfile').prop('files')[0];
          var ext = file_data.name.split('.').pop();
          if(ext == 'docx'){
            var form_data = new FormData();     
            form_data.append('file', file_data);                  
            $.ajax({
                    url: 'upload.php', // point to server-side PHP script 
                    dataType: 'text',  // what to expect back from the PHP script, if anything
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: form_data,                         
                    type: 'post',
                    success: function(php_script_response){
                      var response = $.parseJSON(php_script_response);
                      if(response.status == 'error'){
                         $('.alert-danger').css({'display':'block'});
                         $('.alert-danger').html('file upload failed please try again');
                      }
                      if(response.status == 'success'){
                         $('#myid').html("http://docs.google.com/gview?url=http://loalhost:8888/Grade/uploads/IEP Form-1.docx&embedded=true");
                      }
                    }
             });
          }else{
            $('.alert-danger').css({'display':'block'});
            $('.alert-danger').html('file extension must be docx'); return false;
          }
        });
    </script>

PHP code:

<?php

// A list of permitted file extensions
$allowed = array('docx');

//echo "<pre/>";print_r($_FILES); die;

if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){

    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }

    if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$_FILES['file']['name'])){
        echo '{"status":"success"}';
        exit;
    }
}

echo '{"status":"error"}';
exit;

Now:

I have simple file upload code through jquery and php (working perfectly fine).

Now what I want is, after upload file when it comes success as a response then I want to show a doc file using API to my <div id = "myid" style = "height:500px;width:600px;"></div> (without refresh)

So what I tried:

$('#myid').html("http://docs.google.com/gview?url=http://loalhost:8888/Grade/uploads/IEP Form-1.docx&embedded=true");

(link changed for security reason but docx is available on server and I am able to open it directly)

But obviously it's not going to work.

So how can I do that?

  • 写回答

1条回答 默认 最新

  • dsdapobp26141 2016-07-01 11:53
    关注

    Normally you'd do :

    if(response.status == 'success'){
         $('#myid').load("http://docs.google.com/gview?url=http://loalhost:8888/Grade/uploads/IEP Form-1.docx&embedded=true");
    }
    

    Instead of .html (which does something else).

    However it's unlikely to work because the document you are accessing is not one that is managed by GoogleDocs (there may be cross-origin issues), so make sure you're allowing remote requests from Google to be made.

    Instead you could try

    if(response.status == 'success'){
        $('#myid').html("<iframe src=\"http://docs.google.com/gview?url=http://loalhost:8888/Grade/uploads/IEP Form-1.docx&embedded=true\"></iframe>");
    }
    

    In case Google allows embedding of gview in iframes (though they may not).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角