使用 HTTPS 时不显示上传进度

雷南霍夫曼

我用于上传文件的这段代码工作正常,它显示了进度并完成了工作……但是当我在带有 HTTPS (SSL) 的 Web 服务器上使用它时,文件已上传,但未显示进度。 .. 如果我切换回 HTTP,就会出现进度......有人知道吗?

// 索引.PHP


<!DOCTYPE html>
<html>
<head>
<script>
/* Script written by Adam Khoury @ DevelopPHP.com */
/* Video Tutorial: http://www.youtube.com/watch?v=EraNFJiY0Eg */
function _(el){
    return document.getElementById(el);
}
function uploadFile(){
    var file = _("file1").files[0];
    // alert(file.name+" | "+file.size+" | "+file.type);
    var formdata = new FormData();
    formdata.append("file1", file);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "upload.php");
    ajax.send(formdata);

    console.log('INIT');
}
function progressHandler(event){
    console.log('PROGRESS', event);
    _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
    var percent = (event.loaded / event.total) * 100;
    _("progressBar").value = Math.round(percent);
    _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
    console.log('COMPLETE', event);
    _("status").innerHTML = event.target.responseText;
    _("progressBar").value = 0;
}
function errorHandler(event){
    console.log('ERROR', event);
    _("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
    console.log('ABORT', event);
    _("status").innerHTML = "Upload Aborted";
}
</script>
</head>
<body>
<h2>HTML5 File Upload Progress Bar Tutorial</h2>
<form id="upload_form" enctype="multipart/form-data" method="post">
  <input type="file" name="file1" id="file1"><br>
  <input type="button" value="Upload File" onclick="uploadFile()">
  <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
  <h3 id="status"></h3>
  <p id="loaded_n_total"></p>
</form>
</body>
</html>

// 上传.PHP

    $fileName = $_FILES["file1"]["name"]; // The file name
    $fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
    $fileType = $_FILES["file1"]["type"]; // The type of file it is
    $fileSize = $_FILES["file1"]["size"]; // File size in bytes
    $fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true


    if (!$fileTmpLoc) { // if file not chosen
        echo "ERROR: Please browse for a file before clicking the upload button.";
        exit();
    }


    if(move_uploaded_file($fileTmpLoc, "test_uploads/$fileName")){
        echo "$fileName upload is complete";
    } else {
        echo "move_uploaded_file function failed";
    }

// 服务人员:


var CACHE_STATIC_NAME = 'pensadigital-estatico-v21';
var CACHE_DYNAMIC_NAME = 'pensadigital-dinamico-v21';

// Static cache
const statiAssets = [
    '/',
    '/index.php',
    '/biblioteca/js/jquery-2.1.0.min.js',
    '/biblioteca/js/jquery.form.min.js',
    '/biblioteca/js/jquery.fancybox.pack.js',
    '/biblioteca/js/scripts.js',
    '/biblioteca/js/formularios.js',
    '/biblioteca/templates/base/material_azul/jquery.fancybox.css',
                            '/biblioteca/templates/base/material_azul/estilos.css','/biblioteca/templates/fonte/roboto_condensed/fonte.css',    '/biblioteca/css/estilos.css'
]

// Install SW
self.addEventListener('install', async event=>{
    //console.log('[Service Worker] instalando SW');
    event.waitUntil(
    caches.open(CACHE_STATIC_NAME)
      .then(function(cache) {
        console.log('[Service Worker] Fazendo cache estatico');
        cache.addAll(statiAssets);
      })
    )
})

// Remove old cache files
self.addEventListener('activate', function(event) {
  console.log('[Service Worker] Activating Service Worker ....', event);
  event.waitUntil(
    caches.keys()
      .then(function(keyList) {
        return Promise.all(keyList.map(function(key) {
          if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME) {
            console.log('[Service Worker] Removing old cache.', key);
            return caches.delete(key);
          }
        }));
      })
  );
  return self.clients.claim();
});


// FETCH REQUESTS
self.addEventListener('fetch', event=>{

    url_solicitacao = event.request.url;

    // se a solicitação é de um vídeo, apenas repassar para cliente
    if ( url_solicitacao.indexOf('.mp4') > 0 || url_solicitacao.indexOf('.mp3') > 0 ){

        //console.log('[SW] Vídeo, passar direto: ', url_solicitacao);
        return;


    // Se é uma imagem, adotar estratégia de cache first depois network
    }else if ( url_solicitacao.indexOf('.jpg') > 0 || url_solicitacao.indexOf('.jpeg') > 0 || url_solicitacao.indexOf('.png') > 0 || url_solicitacao.indexOf('.gif') > 0 ){

        event.respondWith(
            caches.match(event.request)
                .then(function (response) {
                if (response) {
                    return response;
                } else {
                    return fetch(event.request)
                    .then(function (res) {
                        return caches.open(CACHE_DYNAMIC_NAME)
                        .then(function (cache) {
                            cache.put(event.request.url, res.clone());
                            return res;
                        })
                    })
                }
                })
        );


    // qualquer outro conteúdo, adotar estratégia de network first depois cache
    }else{

        event.respondWith(
            fetch(event.request)
                .then(function(res) {
                    return caches.open(CACHE_DYNAMIC_NAME)
                            .then(function(cache) {
                                cache.put(event.request.url, res.clone());
                                return res;
                            })
                })
                .catch(function(err) {
                    return caches.match(event.request);
                })
        );

    }

});
雷南霍夫曼

我明白了……我只是对我的 Service Worker 代码进行了更改以忽略 POST 请求,如下所示:

self.addEventListener("fetch", event => {
    //This is the code that ignores post requests
    if (event.request.method === "POST") {
        return;
    }

    // the rest of the code
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用addFooterView时不显示进度栏

来自分类Dev

使用 Google Drive API 时不显示上传的文件

来自分类Dev

使用DialogCoordinator时Mahapps中不显示不确定的进度栏

来自分类Dev

在登录时强制使用https

来自分类Dev

在登录时强制使用https

来自分类Dev

使用HttpWebRequest类上传和下载时显示百分比进度

来自分类Dev

使用预先签名的URL上传到S3时显示进度栏

来自分类Dev

使用Bootstrap进度栏在模态中显示上传进度

来自分类Dev

使用trustStore时HTTPS证书验证失败

来自分类Dev

使用https浏览网站时出现错误

来自分类Dev

强制Django在反转时使用HTTPS URL

来自分类Dev

使用wget下载HTTPS文件时出错

来自分类Dev

使用https浏览网站时出现错误

来自分类Dev

使用代理时如何获取https域

来自分类Dev

使用Java中的Fetch API上传文件并显示进度

来自分类Dev

如何使用C#HttpClient PostAsync显示上传进度

来自分类Dev

使用DataOutputStream上传文件不会显示实际进度

来自分类Dev

在不使用HTTPS时以及不使用HTTPS时,公共VPN服务的数据安全吗?

来自分类Dev

使用System.Net.HttpClient上传时如何跟踪进度?

来自分类Dev

使用Yammer登录时使用的是“ http”而不是“ https”,因此失败并显示“无效的重定向URI”错误

来自分类Dev

将包上传到 pypi 抱怨“必须使用 HTTPS”

来自分类Dev

使用Google帐户登录时使用gitlab的https链接

来自分类Dev

在tableView中使用MBProgressHUD时不显示

来自分类Dev

使用mysqli时不显示弹出窗口

来自分类Dev

使用pack()时不显示按钮

来自分类Dev

使用开放 API 时不显示标题

来自分类Dev

使用 Canvas 时圆圈不显示

来自分类Dev

Vaadin BrowserFrame不显示HTTPS源

来自分类Dev

在IOS中使用Soap访问HTTPS Web服务时显示媒体类型不受支持的错误

Related 相关文章

  1. 1

    使用addFooterView时不显示进度栏

  2. 2

    使用 Google Drive API 时不显示上传的文件

  3. 3

    使用DialogCoordinator时Mahapps中不显示不确定的进度栏

  4. 4

    在登录时强制使用https

  5. 5

    在登录时强制使用https

  6. 6

    使用HttpWebRequest类上传和下载时显示百分比进度

  7. 7

    使用预先签名的URL上传到S3时显示进度栏

  8. 8

    使用Bootstrap进度栏在模态中显示上传进度

  9. 9

    使用trustStore时HTTPS证书验证失败

  10. 10

    使用https浏览网站时出现错误

  11. 11

    强制Django在反转时使用HTTPS URL

  12. 12

    使用wget下载HTTPS文件时出错

  13. 13

    使用https浏览网站时出现错误

  14. 14

    使用代理时如何获取https域

  15. 15

    使用Java中的Fetch API上传文件并显示进度

  16. 16

    如何使用C#HttpClient PostAsync显示上传进度

  17. 17

    使用DataOutputStream上传文件不会显示实际进度

  18. 18

    在不使用HTTPS时以及不使用HTTPS时,公共VPN服务的数据安全吗?

  19. 19

    使用System.Net.HttpClient上传时如何跟踪进度?

  20. 20

    使用Yammer登录时使用的是“ http”而不是“ https”,因此失败并显示“无效的重定向URI”错误

  21. 21

    将包上传到 pypi 抱怨“必须使用 HTTPS”

  22. 22

    使用Google帐户登录时使用gitlab的https链接

  23. 23

    在tableView中使用MBProgressHUD时不显示

  24. 24

    使用mysqli时不显示弹出窗口

  25. 25

    使用pack()时不显示按钮

  26. 26

    使用开放 API 时不显示标题

  27. 27

    使用 Canvas 时圆圈不显示

  28. 28

    Vaadin BrowserFrame不显示HTTPS源

  29. 29

    在IOS中使用Soap访问HTTPS Web服务时显示媒体类型不受支持的错误

热门标签

归档