Mega Code Archive

 
Categories / Delphi / Files
 

UploadFile Function for .NET for SendMail, example

Title: UploadFile Function for .NET for SendMail, example: Question: I need to attach files in my .NET app, but sometimes, the permissions are denied... an uploader file function for .NET would help... Answer: I hope this helps anyone who tryed with no sucessfull to send mails without upload the attachments... thanks Zarko: http://delphi.about.com/od/aspnet/l/aa070604a.htm Function UpLoadFile(Pg:System.Web.UI.Page; FileUpload:System.Web.UI.HtmlControls.HtmlInputFile; ServerSavePath:String; KBLimitSize:LongInt; UseSessionIDinFileName:Boolean):String; Var ClientFileName,ServerFileName,ARQ:String; UploadOK:Boolean; Begin If (Not Assigned(FileUpload.PostedFile)) or (FileUpload.PostedFile.FileName.Length = 0) or (FileUpload.PostedFile.ContentLength = 0) then Begin Result:='Arquivo no selecionado ou inexistente!'; Exit; End; If FileUpload.PostedFile.ContentLength 1024 * KBLimitSize then Begin // No mximo mesmo o .NET aceita 3,24MB Result:='Arquivo muito grande! O Tamanho mximo permitido de ' + IntToStr(KBLimitSize) + 'KB.'; Exit; End; ClientFileName:=Path.GetFileName(FileUpload.PostedFile.FileName); If UseSessionIDinFileName then ARQ:=Path.Combine(ServerSavePath, Pg.Session.SessionID + ClientFileName) Else ARQ:=Path.Combine(ServerSavePath, ClientFileName); If System.IO.File.Exists(ARQ) then System.IO.File.Delete(ARQ); ServerFileName:=ARQ; UploadOK:=True; Try FileUpload.PostedFile.SaveAs(ServerFileName); Except On Ex:Exception do Begin UploadOK:=False; Result:='Ocorreu algum erro na tentativa de salvar o arquivo remotamente: ' + Ex.ToString; End; End; If UploadOK then Result:='OK'; End; []'s Marcos Costa - Brasil