SimpleWebServer icon indicating copy to clipboard operation
SimpleWebServer copied to clipboard

支持中文下载功能

Open wustghj opened this issue 3 years ago • 8 comments

无法中文下载原因: 浏览器请求中文文件时,url会将中文自动编码为unicode,因此在下载中文文件时需要进行解码

解决方法,在httprequest.cpp中增加解码函数

//unicode解码

unsigned char FromHex(unsigned char x)
{
	unsigned char y;
	if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
	else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
	else if (x >= '0' && x <= '9') y = x - '0';
	else assert(0);
	return y;
}


std::string UrlDecode(const std::string& str)
{
	std::string strTemp = "";
	size_t length = str.length();
	for (size_t i = 0; i < length; i++)
	{
		if (str[i] == '+') strTemp += ' ';
		else if (str[i] == '%')
		{
			assert(i + 2 < length);
			unsigned char high = FromHex((unsigned char)str[++i]);
			unsigned char low = FromHex((unsigned char)str[++i]);
			strTemp += high * 16 + low;
		}
		else strTemp += str[i];
	}
	return strTemp;
}

const static std::regex regex_filesPath(".files.+");

然后在parsepath中增加一个条件判断

/*
 解析地址 */
void HttpRequest::parsePath()
{
    if (path == "/") path = "/index.html";
    else if (DEFAULT_HTML.count(path))
        path += ".html";
    else if (path == "/list.json")
    {
        auto files = getFiles("./resources/files");
        Json::Value root;
        Json::Value file;
        for (int i = 0; i < (int)files.size(); i ++)
        {
            file["filename"] = files[i];
            root.append(file);
        }
        write_json("./resources/list.json", root);
    }
    // else if(path == "/files/+")
    else if(regex_match(path,regex_filesPath)){
        // cout<<"匹配到files"<<endl;
        string newpath = "/files/";
        string tobedecode = path.substr(7);
        cout<<tobedecode<<endl;
        newpath+=UrlDecode(tobedecode);
        path = newpath;
    }
}

这样可以解决get请求无法获取中文文件的问题

wustghj avatar Mar 12 '23 07:03 wustghj

image

请问为什么没有下载的地方呢,只有文件名可以看哎

LJD877 avatar Mar 29 '23 08:03 LJD877

这个应该是作者前端代码忘记写了,你把download.html代码替换成这个

<!DOCTYPE html>
<html lang="en">

<head>

     <meta charset="UTF-8">

     <title>DOWNLOAD</title>
     <link rel="icon" href="images/favicon.ico">
     <link rel="stylesheet" href="css/bootstrap.min.css">
     <link rel="stylesheet" href="css/animate.css">
     <link rel="stylesheet" href="css/magnific-popup.css">
     <link rel="stylesheet" href="css/font-awesome.min.css">

     <!-- Main css -->
     <link rel="stylesheet" href="css/style.css">

</head>

<body data-spy="scroll" data-target=".navbar-collapse" data-offset="50">

    <!-- SCRIPTS -->
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/smoothscroll.js"></script>
    <script src="js/jquery.magnific-popup.min.js"></script>
    <script src="js/magnific-popup-options.js"></script>
    <script src="js/wow.min.js"></script>
    <script src="js/custom.js"></script>

    <!-- PRE LOADER -->
    <div class="preloader">
        <div class="spinner">
            <span class="spinner-rotate"></span>
        </div>
    </div>


    <!-- NAVIGATION SECTION -->
    <div class="navbar custom-navbar navbar-fixed-top" role="navigation">
        <div class="container">

            <div class="navbar-header">
                <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon icon-bar"></span>
                        <span class="icon icon-bar"></span>
                        <span class="icon icon-bar"></span>
                </button>
                <!-- lOGO TEXT HERE -->
                <a href="/" class="navbar-brand">DOWNLOAD</a>
            </div>
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                        <li><a class="smoothScroll" href="/">首页</a></li>
                        <li><a class="smoothScroll" href="/picture.html">图片</a></li>
                        <li><a class="smoothScroll" href="/video.html">视频</a></li>
                        <li><a class="smoothScroll" href="/login.html">登录</a></li>
                        <li><a class="smoothScroll" href="/register.html">注册</a></li>
                        <li><a class="smoothScroll" href="/upload.html">上传</a></li>
                        <li><a class="smoothScroll" href="/download.html">下载</a></li>
                </ul>
            </div>

        </div>
    </div>
    <!-- HOME SECTION -->
    <!-- <section id="home">
        <div class="container">
            <div class="row"></div>
                <div align="center">
                    <h2 style="text-align:center;">文件上传</h2>
                    <div id="list"></div>  
                </div>
            </div>
        </div>
    </section>	  -->
    <section id="home">
        <div class="container">
            <div class="row">
                <div align="center">
                    <h1 class="wow fadeInUp" data-wow-delay="0.6s">文件列表</h1>
                </div>
                <div align="left">
                    <ul class="wow fadeInUp" data-wow-delay="0.6s" id="list"> </ul>
                </div>
            </div>
        </div>
    </section>

    <script>
        $.ajax({
            url: "/list.json", //json文件位置
            type: "GET", //请求方式为get
            dataType: "json", //返回数据格式为json
            success: function(data) { //请求成功完成后要执行的方法 
                var str = "";
                //each循环 使用$.each方法遍历返回的数据date
                $.each(data, function(i, item) {
                    str += '<div><font size="4" color="green">' + item.filename + '</font></div>';
                    str += '<a href = "/files/' + item.filename + '" download = "' + item.filename + '">点击下载</a>';
                    str += '<a href = "/files/' + item.filename + '"color = "green" target="_blank">&nbsp&nbsp&nbsp在线预览</a>';
                    // str += '<a href ="D:/test.txt" download ="test.txt">点击下载</a>';
                    $("#list").html(str);
                    // $("#list").text(str);
                })
            }
        })
    </script>
</body>

</html>

wustghj avatar Mar 29 '23 08:03 wustghj

我看了一下后端代码好像也没有下载的功能哎?你部署的时候可以吗

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2023年3月29日(星期三) 下午4:20 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [Sakura1221/SimpleWebServer] 支持中文下载功能 (Issue #2)

这个应该是作者前端代码忘记写了,你把download.html代码替换成这个

LJD877 avatar Mar 29 '23 08:03 LJD877

也是不行,我后来自己改了一下

wustghj avatar Mar 29 '23 08:03 wustghj

大佬,可以用你的项目学习一下吗

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2023年3月29日(星期三) 下午4:46 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [Sakura1221/SimpleWebServer] 支持中文下载功能 (Issue #2)

也是不行,我后来自己改了一下

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

LJD877 avatar Mar 29 '23 08:03 LJD877

好,我待会push到github

wustghj avatar Mar 29 '23 09:03 wustghj

https://github.com/wustghj/SimpleServer

wustghj avatar Mar 29 '23 09:03 wustghj

谢谢哥

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2023年3月29日(星期三) 下午5:10 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [Sakura1221/SimpleWebServer] 支持中文下载功能 (Issue #2)

https://github.com/wustghj/SimpleServer

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

LJD877 avatar Mar 29 '23 09:03 LJD877