PDA

View Full Version : اپلود کردن یک فایل بر روی اینترنت توسط دلفی ؟؟



fire-wizard
شنبه 03 آذر 1386, 23:56 عصر
با سلام خدمت دوستان عزیز .

اگه بخواییم یک برنامه بنویسیم که یک فایل رو بر روی اینترنت ( یک هاست ) اپلود بکنه چه جوری باید عمل کنیم .

توجه : حجم فایل مورد نظر کمتر از 100 کیلوبایت هست .

لطفا راهنمایی کنید . :خجالت:

vcldeveloper
یک شنبه 04 آذر 1386, 01:37 صبح
اگر با پروتکل های اینترنت آشنایی دارید، با استفاده از Indy.
هم می تونید از FTP استفاده کنید، هم از HTTP.

AlirezaBahredar
یک شنبه 04 آذر 1386, 14:02 عصر
http://delphi.about.com/od/delphitips2007/qt/httppost_delphi.htm
برگرفته شده از سایت www.about.com

fire-wizard
دوشنبه 05 آذر 1386, 00:05 صبح
خیلی ممنون دوستان .

اقا علی اگه میشه بر اساس همون کدی که دوستمون گذاشتن یک نمونه درست کنید که واقعا کار بکنه .

خیلی ممنون میشم اگه این لطف رو بکنید .

با تشکر .

fire-wizard
جمعه 16 آذر 1386, 22:56 عصر
بچه ها لینکی که دوستمون داد توش کدهای زیر وجود داشتش .


procedure UploadFilesHttpPost(const wb:TWebBrowser; const URLstring: string; names, values, nFiles, vFiles: array of string) ;
var
strData, n, v, boundary: string;
URL: OleVariant;
Flags: OleVariant;
PostData: OleVariant;
Headers: OleVariant;
idx: Integer;

ms: TMemoryStream;
ss: TStringStream;
begin
if Length(names) <> Length(values) then
raise Exception.Create('UploadFilesHttpPost: Names and Values must have the same length.') ;
if Length(nFiles) <> Length(vFiles) then
raise Exception.Create('UploadFilesHttpPost: FileNames and FileValues must have the same length.') ;

URL := 'about:blank';
Flags := NavNoHistory or NavNoReadFromCache or NavNoWriteToCache or NavAllowAutosearch;
wb.Navigate2(URL, Flags) ;
while wb.ReadyState < READYSTATE_INTERACTIVE do Application.ProcessMessages;

// anything random that WILL NOT occur in the data.
boundary := '---------------------------123456789';

strData := '';
for idx := Low(names) to High(names) do
begin
n := names;
v := values[idx];

strData := strData + '--' + boundary + #13#10 + 'Content-Disposition: form-data; name="' + n + '"' + #13#10#13#10 + v + #13#10;
end;

for idx := Low(nFiles) to High(nFiles) do
begin
n := nFiles[idx];
v := vFiles[idx];

strData := strData + '--' + boundary + #13#10 + 'Content-Disposition: form-data; name="' + n + '"; filename="' + v + '"' + #13#10;

if v = '' then
begin
strData := strData + 'Content-Type: application/octet-stream'#13#10#13#10;
end
else
begin
if (CompareText(ExtractFileExt(v), '.JPG') = 0) or (CompareText(ExtractFileExt(v), '.JPEG') = 0) then
begin
strData := strData + 'Content-Type: image/pjpeg'#13#10#13#10;
end
else if (CompareText(ExtractFileExt(v), '.PNG') = 0) then
begin
strData := strData + 'Content-Type: image/x-png'#13#10#13#10;
end
else if (CompareText(ExtractFileExt(v), '.PDF') = 0) then
begin
strData := strData + 'Content-Type: application/pdf'#13#10#13#10;
end
else if (CompareText(ExtractFileExt(v), '.HTML') = 0) then
begin
strData := strData + 'Content-Type: text/html'#13#10#13#10;
end;


ms := TMemoryStream.Create;
try
ms.LoadFromFile(v) ;
ss := TStringStream.Create('') ;
try
ss.CopyFrom(ms, ms.Size) ;

strData := strData + ss.DataString + #13#10;
finally
ss.Free;
end;
finally
ms.Free;
end;
end;

strData := strData + '--' + boundary + '--'#13#10; [I]// FOOTER
end;

strData := strData + #0;

{2. you must convert a string into variant array of bytes and every character from string is a value in array}
PostData := VarArrayCreate([0, Length(strData) - 1], varByte) ;

{ copy the ordinal value of the character into the PostData array}
for idx := 1 to Length(strData) do PostData := Ord(strData[idx]) ;

[I]{3. prepare headers which will be sent to remote web-server}
Headers := 'Content-Type: multipart/form-data; boundary=' + boundary + #13#10;

{4. you must navigate to the URL with your script and send as parameters your array with POST-data and headers}
URL := URLstring;
wb.Navigate2(URL, Flags, EmptyParam, PostData, Headers) ;
while wb.ReadyState < READYSTATE_INTERACTIVE do Application.ProcessMessages;
end;


خوب حالا من چه جوری باید از این کد استفاده بکنم ؟

در ضمن من روی هاستم HTTP م دارم .

لطفا دوستان کاملا روش استفاده از این کد رو بگن .

با تشکر .