在ftp位置放一些照片。我的控制器正在获取放置在ftp上的某些特定图像的路径列表。我想要做的是使用这些路径将这些图片下载到本地临时文件夹,然后在视图中显示它们。目前我正在努力与模型下载这些图片,但在尝试下载图片时遇到一些问题。下面发布的错误以及当前代码。还缺少文件夹创建代码。
这是我的控制器方法,它会获取路径列表,然后调用下载方法将图片从ftp位置下载到一些临时文件夹(这个临时文件夹应该是我的应用程序,然后视图可以打开该文件夹中的图像:
Function Details(Optional ByVal id As Long = Nothing) As ActionResult
'--Temporary directory for pictures to be downloaded from ftp location (temp_dir) should be created besides application folders
Dim temp_dir As String = "/temp_dir/"
'--If not temp_dir exist ==> create this folder
'--if folder has been created properly go all next lines..
'--Take list of all ftp paths e.g \someFolderForPictures\image1.jpg to be downloaded to temp_dir
Dim PicsTrans As List(Of tblTransPics) = db.tblTransPics.Where(Function(f) f.IdTrans = id).ToList
'--For each picture path download it to temp_dir folder with temporary name
For Each imgFtpPath In PicsTrans
Dim temp_picName As String = Guid.NewGuid.ToString
DownloadFile(imgFtpPath.PicturePath, temp_dir & temp_picName)
Next
'-- If everythings went well - pass either temp_dir path to view and inside view those pictures will be opened or pass list of pictures (bytes)???
Return View(temp_dir)
End Function
下载文件方法:
Public Function DownloadFile(ByVal ftpPicLocation As String, destinationLocation As String) As Boolean
Dim reqFTP As FtpWebRequest
Try
reqFTP = DirectCast(FtpWebRequest.Create("ftp://someftpserver.com" & ftpPicLocation), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential("myusername", "somepassword")
Using outputStream As New FileStream(destinationLocation, FileMode.OpenOrCreate)
Using response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
Using ftpStream As Stream = response.GetResponseStream()
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
End Using
End Using
End Using
Return True
Catch ex As Exception
Throw New Exception("Failed to download", ex.InnerException)
End Try
End Function
此行发生错误:
Using outputStream As New FileStream(destinationLocation, FileMode.OpenOrCreate)
它说:
Cannot find part of path „C:\temp_dir\f0f43089-4a16-4229-8ea6-2c3a55fde2ae”.
顺便提一下C:\?
答案 0 :(得分:0)
删除temp_dir变量中的第一个斜杠。
Dim temp_dir As String = "temp_dir/"
使用此斜杠表示您想要写入根目录。你的根等于C:\ 通过删除第一个斜杠,您可以在应用程序运行的文件夹下获得临时文件夹。