我需要帮助解决我的代码问题。 在我的网站中,我无法解决一个问题。 我通过JavaScript解释代码,创建一个链接,让您下载静态文件夹中请求的文档。 操作如下。
@ App.route ( '/ static / document / <path: name>', methods = [ 'POST'])
def downloads (name): #name is the name of the document
return os.remove (name)
接着我获取了文档,但文件并未被删除。这是下载该文件所需的javascript代码。
downloadlink var = document.createElement ( "a");
d = obj.d; # D is download method before
downloadlink.href = d;
downloadlink.className = "DOWNLOAD_LINK";
downloadlink.download = n;
downloadlink.onClick = setTimeout (function () {location.reload (true);}, 30000);
downloadlink.innerHTML = "<p> Download document" + n + "</ p>";
document.getElementById ( "results"). appendChild (downloadlink);
我哪里做错了吗?
name
是正确的路径,无论是相对于 Python 脚本还是绝对路径?使用绝对路径更容易。在应用程序的配置中,您可以指定类似于basedir = os.path.abspath(os.path.dirname(__file__))
的内容,然后将其用于静态文件路径,如:STATIC_DIR = os.path.join(basedir, 'app/static')
。最终,在操作文件时,您可以像这样做:os.remove(os.path.join(app.config['STATIC_DIR'], name))
。 - vrs