简体   繁体   中英

Possible to temporarily store a file locally before sending to ftp server?

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);
                }

为什么在using语句后不执行file.delete?

您可以调用client.UploadData()从内存上载字节数组,而完全不涉及本地磁盘。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM