【发布时间】: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
-
好吧,或者如果有某种方法只存储文件直到执行另一个命令。