url保存到字符串中未正确保存(vb.net)

时间:2015-03-16 18:35:02

标签: asp.net vb.net azure

我目前在我的代码隐藏

中有这个
Dim PDFDirectory As String = "http://storagename.blob.core.windows.net/pdfs/"
Dim PDFPath As String = PDFDirectory + UploadPDF.FileName
Dim fileNameWithoutExtension As String = System.IO.Path.GetFileNameWithoutExtension(UploadPDF.FileName)
Dim iteration As Integer = 1
While System.IO.File.Exists(Server.MapPath(PDFPath))
    PDFPath = String.Concat(PDFDirectory, fileNameWithoutExtension, "-", iteration, ".pdf")
    iteration += 1
End While   

UploadPDF.SaveAs(Server.MapPath(PDFPath))
e.Values("PDF") = PDFPath

基本上,我将文件从我的网站上传到我的azure存储,工作正常,但上传后我想要上传的文件的网址显示在我的sql数据库上。

问题是,无论何时我上传我的文件,网址显示为 http:/storagename.blob.core.windows.net/pdfs/只有一个' /'在http之后,我得到了这个错误

其他信息:' http:/storagename.blob.core.windows.net/pdfs/PDFfileName.pdf'不是有效的虚拟路径 (请注意,只有一个' /'而不是两个' //'应该在http之后显示,PDFfileName也是PDF的假设名称那刚刚上传)

知道为什么它显示为http:/ storagename而不是http://storagename

3 个答案:

答案 0 :(得分:1)

如果我能看一下代码的其余部分,我可能会有一个小小的想法,为什么它只显示为1“/”...但你总是可以试试这个:

Dim PDFDirectory As String = "http:/" & "/storagename.blob.core.windows.net/pdfs/"

或者为了尝试而添加另一个“/”

Dim PDFDirectory As String = "http://" & "/storagename.blob.core.windows.net/pdfs/"

尝试不伤害:S 一旦我被困了几天,我就错过了一个“\”......想象一下。

答案 1 :(得分:1)

你需要在字符串

之前加上“@”

示例:

 @"http://storagename.blob.core.windows.net/pdfs/PDFfileName.pdf"

答案 2 :(得分:1)

我弄清楚我做错了什么。

这部分

While System.IO.File.Exists(Server.MapPath(PDFPath))
    PDFPath = String.Concat(PDFDirectory, fileNameWithoutExtension, "-", iteration, ".pdf")
    iteration += 1
End While  

正在尝试在本地系统中搜索不存在的文件,因为它是一个URL。

如果有人对此感兴趣,请查看网址是否已存在。 (希望这很有意义)

 Dim PDFDirectory As String = ("http://storagName.blob.core.windows.net/pdfs/")

 'The URL it'll check is the PDFDirectory (URL above) + the name of the file I have on the fileupload control, which has the ID of UploadPDF
 Dim PDFPath As String = PDFDirectory + UploadPDF.FileName
 Dim fileNameWithoutExtension As String = System.IO.Path.GetFileNameWithoutExtension(UploadPDF.FileName)
 Dim iteration As Integer = 1

'Check if URL already exists
 Dim urlTest As New System.Uri(PDFDirectory + UploadPDF.FileName)
 Dim req As System.Net.WebRequest
 req = System.Net.WebRequest.Create(urlTest)
 Dim resp As System.Net.WebResponse

 Try
 'If URL already exists, PDF name will iterate like this PDFname-1.pdf and upload to Azure Storage
 resp = req.GetResponse()
 resp.Close()
 req = Nothing
 PDFPath = String.Concat(PDFDirectory, fileNameWithoutExtension, "-", iteration, ".pdf")
 iteration += 1

 Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(PDFPath)

 Using UploadPDF.PostedFile.InputStream
     blockBlob.UploadFromStream(UploadPDF.PostedFile.InputStream)
 End Using

 Catch ex As Exception
 'If URL doesn't exist then upload PDF to Azure Storage
 Dim blockBlob As CloudBlockBlob = container.GetBlockBlobReference(UploadPDF.FileName)

 Using UploadPDF.PostedFile.InputStream
     blockBlob.UploadFromStream(UploadPDF.PostedFile.InputStream)
 End Using
 End Try

 'This part saves the PDFPath (the URL of the saved PDF) as a value into the SQL database column called "PDF"
 e.Values("PDF") = PDFPath

 'So in my SQL DB in the PDF column will have this value -  http://storagName.blob.core.windows.net/pdfs/PDFname.pdf
 'Or if that URL already exists, it won't overwrite that url but instead have this value - http://storagName.blob.core.windows.net/pdfs/PDFname-1.pdf