【问题标题】:Possible to temporarily store a file locally before sending to ftp server?可以在发送到 ftp 服务器之前在本地临时存储文件吗?
【发布时间】:2013-12-06 13:40:45
【问题描述】:

我有一个用于文件上传的 ASP 控件。当用户发布它时,它首先本地存储在我运行网站的位置,然后我将它复制到远程 ftp 服务器。

但是,是否可以在将其复制到 ftp 服务器后将其从本地服务器中删除?我想把它存储在 ~temp 文件夹中,但我无法让它工作。到目前为止,我需要在我的项目中创建一个名为“temp”的文件夹。有任何想法吗?这里方法:

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

【问题讨论】:

  • 有什么问题?什么不工作?
  • @SLaks 问题只是我只想临时存储文件,这意味着我希望它在复制到 Ftp 服务器后立即从本地服务器中删除。
  • 所以你问如何删除一个文件? google.com/search?q=c%23+delete+file
  • 好吧,或者如果有某种方法只存储文件直到执行另一个命令。

标签: c# asp.net ftp


【解决方案1】:

你可以调用client.UploadData()从内存中上传一个字节数组,完全不涉及你的本地磁盘。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这就是我的选择,感谢您的时间和知识,尽管 SLaks 是第一个 :)
    猜你喜欢
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    相关资源
    最近更新 更多