0
這是我的服務器的編碼請幫助如何發送圖像從客戶端到服務器?
procedure TForm1.IdTCPServerExecute(AThread: TIdPeerThread);
var
InputString: string;
ACommand: string[1];
AFileName: string;
ATempFileName: string;
AFileStream: TFileStream;
begin
InputString := UpperCase(AThread.Connection.ReadLn);
ACommand := Copy(InputString, 1, 1);
AFileName := FPicFilePath + Copy(InputString, 2, 5) + '.jpg';
if ACommand = 'R' then begin
AFileStream := TFileStream.Create(AFileName, fmOpenRead + fmShareDenyNone);
try
AThread.Connection.WriteStream(AFileStream, true, true);
finally
AFileStream.Free;
end;
end else if ACommand = 'S' then begin
ATempFileName := FPicFilePath + 'TEMP.jpg';
if FileExists(ATempFileName) then
DeleteFile(ATempFileName);
AFileStream := TFileStream.Create(ATempFileName, fmCreate);
try
AThread.Connection.ReadStream(AFileStream, -1, false);
//RenameFile(ATempFileName, AFileName);
finally
AFileStream.Free;
end;
end;
AThread.Connection.Disconnect;
end;
這是我的客戶的編碼
procedure TForm1.SendImageToServer(ASendCmd: string);
var
AFileStream: TFileStream;
begin
MessageDlg('Sending ' + ASendCmd + ' :' + FSendFileName, mtInformation, [mbOK], 0);
Screen.Cursor := crHourGlass;
with IdTCPClient do begin
if Connected then Disconnect;
Host := '127.0.0.1';
Port := 2108;
AFileStream := TFileStream.Create(FSendFileName, fmOpenRead);
try
try
Connect;
try
WriteLn(ASendCmd);
WriteStream(AFileStream, true, false);
finally
Disconnect;
end;
finally
AFileStream.Free;
end;
except
end;
end;
Screen.Cursor := crDefault;
end;
我能成功地從服務器獲取圖像,但是當我必須發送一個新的圖像回服務器,我只是有一個空的TEMP.jpg。
請幫忙。 謝謝。
德爾福5,印第9
我懷疑你應該提前發送文件長度。請引用你的indy9幫助文件'AThread.Connection.ReadStream' –
我認爲只有客戶端應該調用'Disconnect'方法。爲什麼在服務器端和客戶端使用'Disconnect'? – AndreaBoc
@ Arioch'The:OP在服務器端將WriteStream()的AWriteByteCount參數設置爲true,所以它提前發送文件長度,但是他在客戶端沒有做同樣的事情,儘管服務器期待它。 –