Guest User

Untitled

a guest
Jan 16th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. /// <summary>
  2. /// Method that uploads sample to ftp
  3. /// </summary>
  4. private void UploadSample()
  5. {
  6. String id = Request.QueryString["ID"];
  7. String path = Server.MapPath("~/temp/");
  8. String filename = Path.GetFileName(fuSample.PostedFile.FileName);
  9.  
  10. try
  11. {
  12. if (
  13. fuSample.PostedFile.ContentType == "application/pdf"
  14. )
  15. {
  16. fuSample.PostedFile.SaveAs(path + fuSample.FileName);
  17.  
  18. path += fuSample.FileName;
  19.  
  20. String ftpServer = "ftp://130.238.174.237:2121";
  21.  
  22. String userName = "ftpuser";
  23. String password = "KHR3ENJjMh";
  24.  
  25. String uri = "ftp://130.238.174.237:2121/" + id;
  26.  
  27. if (!DirectoryExists(uri + "/", userName, password))
  28. {
  29. // Mappen inte finns
  30. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  31. request.Method = WebRequestMethods.Ftp.MakeDirectory;
  32. request.Credentials = new NetworkCredential(userName, password);
  33. using (var resp = (FtpWebResponse)request.GetResponse())
  34. {
  35. WebClient client = new WebClient();
  36. client.Credentials = new NetworkCredential(userName, password);
  37. client.UploadFile(ftpServer + "/" + id + "/" + new FileInfo(path).Name, "STOR", path);
  38. backEnd.InsertSampleToDb(fuSample.FileName, id);
  39. resp.Close();
  40. }
  41. }
  42. else
  43. {
  44. // Mappen finns redan
  45. WebClient client = new WebClient();
  46. client.Credentials = new NetworkCredential(userName, password);
  47. client.UploadFile(uri + "/" + new FileInfo(path).Name, "STOR", path);
  48. backEnd.InsertSampleToDb(fuSample.FileName, id);
  49. }
  50.  
  51. if (File.Exists(path))
  52. {
  53. File.Delete(path);
  54. }
  55. }
  56. else
  57. {
  58. lblFeedback.Text = "Ej tillåten filtyp";
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. lblFeedback.Text = "Fel vid uppladdning";
  64. }
  65. }
Add Comment
Please, Sign In to add comment