Django 提供三种方式实现文件下载功能,分别是:HttpResponse、StreamingHttpResponse和FileResponse,三者的说明如下:
HttpResponse 是所有响应过程的核心类,它的底层功能类是HttpResponseBase。
StreamingHttpResponse 是在HttpResponseBase 的基础上进行继承与重写的,它实现流式响应输出(流式响应输出是使用Python的迭代器将数据进行分段处理并传输的),适用于大规模数据响应和文件传输响应。
FileResponse 是在StreamingHttpResponse 的基础上进行继承与重写的,它实现文件的流式响应输出,只适用于文件传输响应。
这里推荐一个github上的一个项目,可以免扣分下载文件
源码地址:https://gitcode.com/cuckooplus/csdn_download
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
Label2: TLabel;
Button2: TButton;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses MsXML;
<br>{ Code By Noevil -- Thank you! }{ 分割字符串的函数, 其实是个过程 }
procedure Split(const str: string; const c: Char; var List: TStrings);
begin
List.Clear;
List.Delimiter := c;
List.DelimitedText := str;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
SoftUrl: IXMLHTTPRequest;
Url: String;
Temp: String;
List: TStrings;
begin
{ 取得ID }
Url :=
'http://download.csdn.net/index.php/rest/source/getsourceinfo/' +
Edit1.Text;
{ 利用bug }
SoftUrl := CoXMLHTTP.Create; // 较早的版本或许是 SoftUrl := CoXMLHTTPRequest.Create;
SoftUrl.open('Get', Url, False, EmptyParam, EmptyParam);
SoftUrl.send(EmptyParam);
{ 凶猛的替换 }
Temp := StringReplace(SoftUrl.responseText, '"', '', [rfReplaceAll]);
Temp := StringReplace(Temp, '\', '', [rfReplaceAll]);
Temp := StringReplace(Temp, '{', '', [rfReplaceAll]);
Temp := StringReplace(Temp, '}', '', [rfReplaceAll]);
Temp := StringReplace(Temp, 'url:', ';', [rfReplaceAll]);
Temp := StringReplace(Temp, ' ', '', [rfReplaceAll]);
Temp := StringReplace(Temp, 'http#', 'http:', [rfReplaceAll]);
Memo1.Text := Temp;
{ 提取下载地址链接 }
List := TStringList.Create;
Split(Memo1.Text, ';', List);
Memo1.Text := List[1];
List.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
SoftUrl: IXMLHTTPRequest;
Url: String;
Temp: String;
List: TStrings;
begin
{ 取得ID }
Url :=
'http://download.csdn.net/index.php/rest/source/getsourceinfo/' +
Edit1.Text;
{ 利用bug }
SoftUrl := CoXMLHTTP.Create; // 较早的版本或许是 SoftUrl := CoXMLHTTPRequest.Create;
SoftUrl.open('Get', Url, False, EmptyParam, EmptyParam);
SoftUrl.send(EmptyParam);
{ 凶猛的替换 }
Temp := StringReplace(SoftUrl.responseText, '"', '', [rfReplaceAll]);
Temp := StringReplace(Temp, '\', '', [rfReplaceAll]);
Temp := StringReplace(Temp, '{', '', [rfReplaceAll]);
Temp := StringReplace(Temp, '}', '', [rfReplaceAll]);
Temp := StringReplace(Temp, 'url:', ';', [rfReplaceAll]);
Temp := StringReplace(Temp, ' ', '', [rfReplaceAll]);
Temp := StringReplace(Temp, 'http#', 'http:', [rfReplaceAll]);
Memo1.Text := Temp;
{ 提取下载地址链接 }
List := TStringList.Create;
Split(Memo1.Text, ';', List);
Memo1.Text := List[1];
List.Free;
ShellExecute(Handle, 'open', 'IExplore.EXE', PWideChar(Memo1.Text), nil,
SW_SHOWNORMAL);
end;
end.