如何在使用Django更改<input type='file'>时获取所选文件的完整路径

2024-10-03 17:26:53 发布

您现在位置:Python中文网/ 问答频道 /正文

compress.html

 <form action="/compress" method="POST"  enctype="multipart/form-data">
            {% csrf_token %}
            <label for="pdf">Select pdf :</label>
            <input type="file" name="pdf1" id ="pdf1" accept="pdf/*" />
            <br>
            <button type="submit" class="btn btn-dark mt-2"  value="click">
             submit
            </button>
            <script type="text/javascript">
            function getFilePath(){
            $('input[type=file]').change(function () {
            var filePath=$('#fileUpload').val(); 
             });
             }
            </script>

views.py

  def mergepdf(request):
    from pylovepdf.ilovepdf import ILovePdf
    ilovepdf = ILovePdf('my_secrete_key', verify_ssl=True)
    task = ilovepdf.new_task('compress')
    task.request.FILES['full_path']# here i want full path of selected file 
    task.set_output_folder('/Downloads/download_pdffile')
    task.execute()
    task.download()
    task.delete_current_task()

filePath变量包含选定文件的唯一名称,而不是完整路径。 我在网上搜索过,但出于安全原因,浏览器(FF、chrome)似乎只给出了文件名。 是否有其他方法获取所选文件的完整路径


Tags: formtaskinputpdftypescriptfunctionbutton