0

I've got an ASP control for file upload. When the user posts it, it's first locally stored on where I run the website and then I copy it to a remote ftp server.

However, is it possible to remove it from the local server once it's been copied to the ftp server? I'm thinking like storing it in a ~temp folder, but I can't get that to work. As of now I need to create a folder within my project called "temp". Any ideas? Here the method:

            String id = Request.QueryString["ID"];
            String path = Server.MapPath("~/temp/");
            String filename = Path.GetFileName(fuPicture.PostedFile.FileName);

            if (fuPicture.HasFile)
            {
                try
                {
                    if (
                        fuPicture.PostedFile.ContentType == "image/jpeg" ||
                        fuPicture.PostedFile.ContentType == "image/png" ||
                        fuPicture.PostedFile.ContentType == "image/gif"
                        )
                    {
                        fuPicture.PostedFile.SaveAs(path + fuPicture.FileName);
                    }
                    else 
                    {
                        lblFeedback.Text = "Not allowed file extension";
                    }
                }
                catch (Exception ex)
                {
                    lblFeedback.Text = "Error when uploading";
                }
                path += fuPicture.FileName;

                String ftpServer = "ftp://xxxx:xxxx";

                String userName = "xx";
                String password = "xx";

                FtpWebRequest request = 
                    (FtpWebRequest)WebRequest.Create(new Uri("ftp://xxxx:xxxx/" + id));
                request.Method = WebRequestMethods.Ftp.MakeDirectory;
                request.Credentials = new NetworkCredential(userName, password);
                using (var resp = (FtpWebResponse)request.GetResponse()) 
                {
                    WebClient client = new WebClient();
                    client.Credentials = new NetworkCredential(userName, password);
                    client.UploadFile(ftpServer + "/" + id + "/" + 
                        new FileInfo(path).Name, "STOR", path);
                }
guitarzero
  • 545
  • 9
  • 18
  • What's the problem? What isn't working? – SLaks Dec 06 '13 at 13:42
  • @SLaks The problem is just that I only want to store the file temporarily, meaning I want it gone from the local server as soon as it's copied to the Ftp server. – guitarzero Dec 06 '13 at 13:48
  • So you're asking how to delete a file? https://www.google.com/search?q=c%23+delete+file – SLaks Dec 06 '13 at 13:49
  • Well, either that or if there's some sort of method that only stores the file until another command is executed. – guitarzero Dec 06 '13 at 13:50

2 Answers2

0

Why you don't do a file.delete after the using statement?

Tunisiano32
  • 180
  • 1
  • 8
0

You can call client.UploadData() to upload a byte array from memory, without involving your local disk at all.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964