Al

Al

  • NA
  • 30
  • 6.3k

FileUpload control - files disappears

Apr 16 2020 12:00 PM
Also, I have a question regarding attached file control
 
<asp:FileUpload ID="FileUpload" runat="server" Enabled="true" AllowMultiple="true" Width="449px" />
 
The idea is the following:
 
I created a contact us page, where a visitor will enter his comments and may or may not attached a file. Once the visitor click on submit.
 
Two things should happen.
 
Email will be sent to admin with attachments // this works fine
 
Attached file is inserted to database table. // posted files disappear ):
 
The problem I am facing is when I consumed “FileUpload” object when I send email, the code in the method below shows no posted files. however, where I comment out the method that send attachment to email. The postedFiles in the method below contains all the file.
 
I am not sure if I cannot consume FileUpload.PostedFiles twice. only one time it works, but when I want to consume it twice, by calling email attachment and saving attachment to database it gives me posted files is zero.
please advise and explain thanks
 
Here is the code that insert the file to database>>>
  1. protected int pushEmailAttachmentsToDb(string tableName, int intDocOwnerId)  
  2. {  
  3. int rtn = 0;  
  4. try  
  5. {  
  6. string mySql = @" insert into " + tableName + "(docOwnerId ,DocName,ContentType,DocData) " +  
  7. " values(@docOwnerId, @DocName, @ContentType, @DocData)";  
  8. // save files to DB  
  9. foreach (HttpPostedFile postedFile in FileUpload.PostedFiles) // here is the issue, postedFiles is empty.  
  10. // foreach (HttpPostedFile postedFile in myUploadedFiles.)  
  11. {  
  12. string filename = Path.GetFileName(postedFile.FileName);  
  13. string contentType = postedFile.ContentType;  
  14. using (Stream fs = postedFile.InputStream)  
  15. {  
  16. using (BinaryReader br = new BinaryReader(fs))  
  17. {  
  18. byte[] bytes = br.ReadBytes((Int32)fs.Length);  
  19. CRUD DocInsert = new CRUD(); // added Nov 2017  
  20. Dictionary<string, Object> p = new Dictionary<stringobject>();  
  21. p.Add("@docOwnerId", intDocOwnerId); // mySenderId  
  22. p.Add("@DocName", filename);  
  23. p.Add("@ContentType", contentType);  
  24. p.Add("@DocData", bytes);  
  25. rtn = DocInsert.InsertUpdateDelete(mySql, p);  
  26. }  
  27. }  
  28. }  
  29. return rtn;  
  30. }  
  31. catch (Exception ex)  
  32. {  
  33. lblOutput.Text = ex.ToString()  
  34. return rtn;  
  35. }  
  36. }
for more information, click the link below
 
https://kfmcvlos-my.sharepoint.com/:f:/g/personal/ahameed_kfmc_med_sa/Eg1OuohHZfhBgVsU8qQTNjwBaxBVn67JvU_0BO5o0gtRmw?e=MGPjza
 
thanks

Answers (3)