nginx搭建文件服务器获取上传进度

接上篇blog,主要讲讲获取上传进度的实现。 参考地址:https://github.com/masterzen/nginx-upload-progress-module

按照其中的说明配置nginx,将其中的例子存成test.html文件。放到nginx 的html目录下。 浏览器运行发现进度条不会变。 经过调试,发现nginx中缺少如下配置:


test.html里面缺少<script type="text/javascript" language="javascript"></scpript>

修改后重新测试:


成功获得上传进度。

nginx配置:

#user  nobody;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    upload_progress proxied 8m;

    #gzip  on;

    server {
        listen  36888;
        server_name  localhost;
	client_max_body_size 200m;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root  html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /upload {
 	    upload_pass /api/upload;
    	    upload_cleanup 400 404 499 500-505;
            upload_store /tmp/upload_tmp;
            #upload_store_access user:r;
            upload_limit_rate 0;
            upload_set_form_field "file_name" $upload_file_name;
            upload_set_form_field "content_type" $upload_content_type;
            upload_set_form_field "tmp_path" $upload_tmp_path;
            upload_aggregate_form_field "md5" $upload_file_md5;
            upload_aggregate_form_field "size" $upload_file_size;
            upload_pass_form_field "^.*$";
            track_uploads proxied 30s;
	}
	
	location /api/upload {
	    proxy_pass   http://192.168.1.56:8888;
        }

	location ~ (.*)/x-progress-id:(\w*) {  
            rewrite ^(.*)/x-progress-id:(\w*)   $1?X-Progress-ID=$2;  
        }  

	location ^~ /progress {
	#此url来获取进度信息
	    report_uploads proxied;
	}

	location /imgs {
	    root /root/tools;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


test.html:

<form id="upload" enctype="multipart/form-data" 
    action="/upload" method="post" 
    οnsubmit="openProgressBar(); return true;">
  <input type="hidden" name="MAX_FILE_SIZE" value="30000000"  />
  <input name="userfile" type="file" label="fileupload" />
  <input type="submit" value="Send File" />
  </form>

And a progress bar to visualize the progress:

  <div>
   <div id="progress" style="width: 400px; border: 1px solid black">
    <div id="progressbar" 
       style="width: 1px; background-color: black; border: 1px solid white">
      
    </div>
   </div>
   <div id="tp">(progress)</div>
  </div>

Then we need to generate the Unique Identifier and launch the upload on submit
action. This also will start the ajax progress report mechanism.

<script type="text/javascript" language="javascript">
 interval = null;

function openProgressBar() {
 /* generate random progress-id */
 uuid = "";
 for (i = 0; i < 32; i++) {
  uuid += Math.floor(Math.random() * 16).toString(16);
 }
 /* patch the form-action tag to include the progress-id */
 document.getElementById("upload").action="/upload?X-Progress-ID=" + uuid;

 /* call the progress-updater every 1000ms */
 interval = window.setInterval(
   function () {
     fetch(uuid);
   },
   1000
 );
}

function fetch(uuid) {
 req = new XMLHttpRequest();
 req.open("GET", "/progress", 1);
 req.setRequestHeader("X-Progress-ID", uuid);
 req.onreadystatechange = function () {
  if (req.readyState == 4) {
   if (req.status == 200) {
    /* poor-man JSON parser */
    var upload = eval(req.responseText);

    document.getElementById('tp').innerHTML = upload.state;

    /* change the width if the inner progress-bar */
    if (upload.state == 'done' || upload.state == 'uploading') {
     bar = document.getElementById('progressbar');
     w = 400 * upload.received / upload.size;
     bar.style.width = w + 'px';
    }
    /* we are done, stop the interval */
    if (upload.state == 'done') {
     window.clearTimeout(interval);
    }
   }
  }
 }
 req.send(null);
}
</script>



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Windows 中搭建一个支持上传和下载文件文件服务器,可以使用 Nginx 和一些插件来实现。以下是步骤: 1. 下载 Nginx for Windows,可以从官网下载:http://nginx.org/en/download.html 2. 解压缩下载的 Nginx 文件到某个目录下,例如 C:\nginx。 3. 在 C:\nginx\conf 目录下创建一个名为 nginx.conf 的文件,并在其中添加以下配置: ``` worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root C:/nginx/html; index index.html index.htm; autoindex on; } location /upload { client_max_body_size 100m; alias C:/nginx/upload/; autoindex on; dav_methods PUT DELETE MKCOL COPY MOVE; dav_access user:rw group:rw all:r; } } } ``` 这个配置文件中定义了一个 http 服务,监听 80 端口,其中 /upload 路径是用来上传文件的,其它路径是用来下载文件的。注意修改 root 和 alias 的路径为你自己的路径。 4. 在 C:\nginx\html 目录下创建一个名为 index.html 的文件,用于测试下载文件是否成功。 5. 在 C:\nginx\upload 目录下创建一个名为 index.html 的文件,用于测试上传文件是否成功。 6. 启动 Nginx,打开命令行窗口,切换到 C:\nginx 目录下,执行命令:nginx.exe。 7. 打开浏览器,访问 http://localhost/,应该可以看到 index.html 页面,用于测试下载文件是否成功。 8. 打开浏览器,访问 http://localhost/upload/,应该可以看到 index.html 页面,用于测试上传文件是否成功。 现在你的文件服务器已经搭建好了,可以用来上传和下载文件了。上传文件时,可以使用 PUT 方法,例如使用 curl 命令上传文件: ``` curl -T file.txt http://localhost/upload/ ``` 下载文件时,可以使用 GET 方法,例如使用浏览器访问 http://localhost/file.txt。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yongche_shi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值