我使用Dropbox API将文件上传到Dropbox,然后生成可共享的链接并将其发送给用户。
但问题是我想强制下载文件,而不是通过dropbox共享链接进行预览。
我知道我可以在链接末尾强制下载设置import tkinter as tk
from tkinter.ttk import Frame, Button
labels = []
entries = []
class Application(Frame):
def __init__(self)
super().__init__()
self.mainFrame()
def mainFrame(self):
self.master.title("Setup")
self.pack(fill=tk.BOTH, expand=True)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = tk.Label(self, text="Follow the instructions on each page", bg="snow2")
lbl.grid(sticky=tk.W, pady=4, padx=5)
area = tk.Frame(self, bg="white")
area.grid(row=1, column=0, columnspan=3, rowspan=4,
padx=5, sticky=tk.E + tk.W + tk.S + tk.N)
# ----------Inside White Box ---------------------
lbl = tk.Label(area, text="Select the number of parts to create:")
lbl.grid(row=1, column=0)
choices = {0, 3, 4, 5, 6, 7, 8, 9, 10}
node_count = tk.IntVar()
node_count.set(0)
node_select = tk.OptionMenu(area, node_count, *choices,
command=lambda x: self.display_change(area, node_count.get()))
node_select.grid(row=1, column=2)
# -----------Outside Part-------------------------
abtn = Button(self, text="Thing 1")
abtn.grid(row=1, column=3, sticky=tk.W)
cbtn = Button(self, text="Thing 2")
cbtn.grid(row=2, column=3, pady=4, sticky=tk.W)
abtn2 = Button(self, text="Thing 3")
abtn2.grid(row=3, column=3, sticky=tk.W + tk.N)
cbtn2 = Button(self, text="Thing 4")
cbtn2.grid(row=3, column=3, pady=28, sticky=tk.W + tk.N)
hbtn = Button(self, text="Exit")
hbtn.grid(row=5, column=2, sticky=tk.W)
sbtn = Button(self, text="Save")
sbtn.grid(row=5, column=3, pady=3, sticky=tk.W)
sbtn = Button(self, text="Help")
sbtn.grid(row=5, column=0, sticky=tk.W)
def display_change(self, area, nodes):
"""Here is where the display is changed so what the user choose is correctly displayed"""
lower_label = tk.Label(area, text="Enter the value of each part")
lower_label.grid(row=2, column=0, sticky=tk.N + tk.W)
global labels, entries
for label in labels:
label.destroy()
for entry in entries:
entry.destroy()
labels = []
entries = []
# This loop creates the correct number of entry box's and labels. Each entry is stored separately
for i in range(nodes):
if nodes <= 4:
labels.append(tk.Label(area, text="Part "+str(i+1)))
labels[i].place(x=10+(120*i), y=55)
entries.append(tk.Entry(area, text="Change"))
entries[i].place(x=10 + (120 * i), y=80, width=100)
else:
labels.append(tk.Label(area, text="part " + str(i + 1)))
labels[i].place(x=10 + (120 * i), y=105)
entries.append(tk.Entry(area, text="Change"))
entries[i].place(x=10 + (120 * i), y=160, width=100)
if __name__ == "__main__":
root = tk.Tk()
"""Calculate center of screen so popup is center"""
w = 650
h = 400
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
# This line prevents the user from changing the size of the window
root.resizable(width=False, height=False)
app = Application(root)
root.mainloop()
,但dropbox生成的可共享链接会返回?dl=1
代码:
?dl=0
我正在使用Dropbox API的V1,很快就会弃用,但现在我需要使用它。
答案 0 :(得分:0)
使用dl=1
网址参数是强制在这些链接上下载的正确方法。您可以在此处找到更多相关信息:
https://www.dropbox.com/help/desktop-web/force-download
要正确执行此操作,您应使用实际的URL解析器来解析URL。然后,如果它已经有dl
参数,请将其设置为1
。如果没有,则将dl
参数集添加到1
。
答案 1 :(得分:0)
您可以利用流,只需将下载流重定向到客户端即可。以下是使用我的简约dropbox-v2-api包装和hapijs路由配置的示例:
{
path: '/getFile',
method: 'GET',
handler: (request, response) => {
dropbox({
resource: 'files/download',
parameters: {
path: '/dropbox/image.jpg'
}
}, (err, result) => {
//download completed
}).pipe(response); //piping file stream
}
}