HTML implements file upload and HTML implements opening file directory

There are two places to pay attention to here:

  • The form submission method needs to be post
  • form adds an attribute as enctype="multipart/form-data"

Add input tag to index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>hello worlds</h1>
<form action="/klvchen/" method="post" enctype="multipart/form-data">
    <p><input type="file" name="upload"></p>
    <p><input type="submit" value="submit"></p>
</form>

</body>
</html>

Modify views.py

from django.shortcuts import render

def klvchen(req):
    print("前端数据: ", req.POST)
    print("file:", req.FILES)

    for item in req.FILES:
        obj = req.FILES.get(item)      # 获取要写入的文件
        filename = obj.name            # 获取文件名
        f = open(filename, 'wb')
        for line in obj.chunks():      # 分块写入
            f.write(line)
        f.close()

    return render(req, "index.html")

File uploaded successfully
 

(5 messages) HTML upload file_weixin_30500289's blog - CSDN blog icon-default.png?t=M0H8https://blog.csdn.net/weixin_30500289/article/details/95012921


 Browse folders in HTML


The specific point is that when you open this html, you can see a folder, click on the folder, and the page jumps to display the contents of a directory in my local computer.
I will only write a small part, how to display the contents of a folder on the local computer after clicking?
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="main" style="height: 100%; text-align: center;"><!-- BEGINOF main -->
<img src="/icons/logo.png" class="logo" />
<pre></pre>
<div id="dirlist"><!-- BEGINOF dirlist -->
<table>
<thead class="headings">
<tr>
<td style="width: 1px;"> </td>
<td>
<a href="?order=N">Name</a>
</td>
<td>
<a href="?order=s">Size</a>
</td>
</tr>
</thead>

<tbody>
<tr>
<td style="width: 1px;">
<img class="icon" src="/icons/folder.png" alt="[DIR]" />
</td>
<td>
<a class="link" href="VEDIO/">VEDIO</a>
</td>
<td class="size">
<span class="size">-</span><span class="unit"></span>
</td>
</tr>
</tbody>
</table>

</div><!-- ENDOF dirlist -->
</div><!-- ENDOF main-->
</body>
</html> 

Quoted from: HTML upload file - klvchen - blog garden (cnblogs.com) icon-default.png?t=M0H8https://www.cnblogs.com/klvchen/p/10168174.html

Guess you like

Origin blog.csdn.net/u014331212/article/details/122893286