这与我的previous question有关。向导控件出现了一个无法预料的问题。
我现在知道如何上传到FTP,但是当在向导控件中使用FileUpload控件时,当您转到下一步时,您选择的文件 <由于回发,强>被清除。我需要能够在上传之前根据向导的结果重命名文件。所以......
我怀疑我需要遵循这样的程序,在FileUpload旁边有一个上传按钮
我想我理解这个过程,所以我想帮助解决如何将FTP上传功能分成两部分,并在向导失败时正确处理错误。
如果您使用以下代码作为基础,那将是一个很大的帮助。一如既往地谢谢:)
Protected Sub UploadFile(ByVal NewFilename As String)
Dim myFtpWebRequest As FtpWebRequest
Dim myFtpWebResponse As FtpWebResponse
'Function one? - Problem, "NewFilename" depends on the output of the Wizard,
' but obviously it has not been called yet.
myFtpWebRequest = CType(WebRequest.Create(ftpServer + ftpPath + NewFilename), FtpWebRequest)
myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
myFtpWebRequest.UseBinary = True
Dim myFileStream As Stream = FileUpload1.FileContent
myFtpWebRequest.ContentLength = myFileStream.Length
'Function two?
Dim requestStream As Stream = myFtpWebRequest.GetRequestStream()
myFileStream.CopyTo(requestStream)
requestStream.Close()
myFtpWebResponse = CType(myFtpWebRequest.GetResponse(), FtpWebResponse)
myFtpWebResponse.Close()
End Sub
以下是基于Icarus输入的最终实现:)
为简洁起见,我排除了错误捕获。
'This function is what kicks things off...
Protected Sub UploadFileToWebServer() Handles btnUploadFile.Click
Dim TempDir As String = "C:\TEMP", FileName As String = "uploadedfile.tmp", FilePath As String
If Not Directory.Exists(TempDir) Then
Directory.CreateDirectory(TempDir).Attributes = FileAttributes.Directory
End If
FilePath = TempDir + "\" + FileName
Session.Add("FileName", File1.FileName) 'Keep track of uploaded file name
File1.SaveAs(FilePath)
Session.Add("File", FilePath)
End Sub
将文件上传到Web服务器后,我们可以继续完成向导,单击“完成”按钮后,向导数据将提交给数据库。文件名基于插入的记录ID。插入记录后,通过“最终”按钮单击调用以下函数,最后将文件上传到FTP服务器,文件名也相应更改。
Protected Sub UploadFileToFtpServer(ByVal FileLinkStr As String)
Dim myFtpWebRequest As FtpWebRequest
Dim myFtpWebResponse As FtpWebResponse
'Defines the filename, path, and upload method, and connection credentials
myFtpWebRequest = CType(WebRequest.Create(ftpServer + ftpPath + FileLinkStr), FtpWebRequest)
'Be sure to authenticate prior to uploading or nothing will upload and no error
myFtpWebRequest.Credentials = New NetworkCredential(ftpUsername, ftpPassword)
myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
myFtpWebRequest.UseBinary = True
'Streams the file to the FTP server
'Retrieves File temporarily uploaded to the Web Server during Wizard Processing
Dim iStream As New FileInfo(Session.Item("File"))
Dim myFileStream As Stream = iStream.OpenRead
myFtpWebRequest.ContentLength = myFileStream.Length
Dim requestStream As Stream = myFtpWebRequest.GetRequestStream()
myFileStream.CopyTo(requestStream)
requestStream.Close()
myFtpWebResponse = CType(myFtpWebRequest.GetResponse(), FtpWebResponse)
myFtpWebResponse.Close()
End Sub
答案 0 :(得分:1)
您的理解是正确的。一旦将文件上传到Web服务器(您需要将其放在某个临时目录中并跟踪您提供的文件名)并且向导成功完成,您将获取该文件,相应地重命名并上传到ftp服务器。如果失败,只需致电:
File.Delete(Path_to_file_uploaded_on_temp_directory);
例如,您可以通过将其存储在Session中来跟踪最初给出的文件名。最初将文件上传到服务器时,请执行Session["FileName"]=Path_to_temp_directory+fileName;
在向导的最后一步,从Session获取文件名,然后重命名并将其上传到FTP服务器或删除它。
当然,您需要考虑可能的名称冲突等。例如,您可以使用Guid为文件生成随机名称。
我希望我能清楚地解释清楚。
修改强>
确保我理解正确......
Wizard
类型的所有步骤基于以上所述,我的建议是:
当用户在他从计算机中选择文件的步骤中单击“下一步”时,您需要立即将文件保存到 Web服务器上的临时位置。您可以通过执行以下操作将文件保存到Web服务器上的此临时文件夹中:
if(FileUpload1.HasFile) //user selected a file
{
try
{
//D:\temp is a temp directory on the Web Server
FileUpload1.PostedFile.SaveAs(@"D:\temp\"+FileUpload1.FileName);
//Store the FULL PATH TO the file just uploaded on Session
Session["FileName"]="D:\temp\"+FileUpload1.FileName;
}
catch (Exception ex)
{
//Handle it.
}
}
在向导的最后一步,假设一切都成功,请执行此操作
Dim myFtpWebRequest As FtpWebRequest
Dim myFtpWebResponse As FtpWebResponse
' You know the NewFileName because it's the output of the wizard
myFtpWebRequest = CType(WebRequest.Create(ftpServer + ftpPath + NewFilename), FtpWebRequest)
myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
myFtpWebRequest.UseBinary = True
'Here you need to read the Original File
Dim myFileStream As Stream = new FileStream(Session["FileName"]),FileMode.Open,FileAccess.Read,FileShare.ReadWrite)
myFtpWebRequest.ContentLength = myFileStream.Length
Dim requestStream As Stream = myFtpWebRequest.GetRequestStream()
myFileStream.CopyTo(requestStream)
requestStream.Close()
myFtpWebResponse = CType(myFtpWebRequest.GetResponse(), FtpWebResponse)
myFtpWebResponse.Close()
如果您决定删除用户上传的原始文件,因为他没有成功完成向导,您只需执行以下操作:
try
{
File.Delete (Session["FileName"]);
}
catch(Exception ex)
{
//Handle it.
}