I am trying to download a ftp file from my online webserver. The file resides in ftp100-0 and is the only file in there. The error code states:
The requested URI is invalid for this FTP command.
Here is my code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
' Get the object used to communicate with the server.
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://www.mysite.com/"), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
' This example assumes the FTP site uses anonymous logon.
request.Credentials = New NetworkCredential("ftp100-0", "password")
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
Console.WriteLine(reader.ReadToEnd())
Console.WriteLine("Download Complete, status {0}", response.StatusDescription)
reader.Close()
response.Close()
End Sub
Try the URL you are providing from a web browser to ensure it is correct. The file name is missing from the URL.
Related
Chrome has a setting that blocks the opening of local files. The specific error you get is Not allowed to load local resource: file:///<file name>. I'm trying to develop an internal site for us to access these files but as many users are using Chrome, I need a workaround to serve these files.
Currently, to get my files and display them, I have this logic:
For Each file As String In Directory.GetFiles("<file path>")
fileInfo = New FileInfo(file)
fileList.Add(fileInfo)
Next
fileList.Sort(Function(x, y) x.Name.CompareTo(y.Name))
For i As Integer = 0 To fileList.Count - 1
pnlLinks.Controls.Add(New LiteralControl("<br />"))
pnlLinks.Controls.Add(New LiteralControl("" & fileList(i).Name & ""))
pnlLinks.Controls.Add(New LiteralControl("          "))
pnlLinks.Controls.Add(New LiteralControl("<span>" & GetByteSize(fileList(i).Length.ToString) & " </span>"))
pnlLinks.Controls.Add(New LiteralControl("<br />"))
Next
The a href is a little funky because I've been experimenting with different folder structures to get this working, but it should not be relevant.
I had the idea of implementing a function that could send this to a byte stream but I'm not experienced in that area and wouldn't be sure how to implement it.
I was able to resolve this with a FileStream and launching to a new page that handles it on Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim pdfFileStream As FileStream = Nothing
Dim pdfFileSize As Long = 0
Dim pdfPath As String = "<file path>"
pdfFileStream = New FileStream(pdfPath, FileMode.Open)
pdfFileSize = pdfFileStream.Length
Dim Buffer(CInt(pdfFileSize)) As Byte
pdfFileStream.Read(Buffer, 0, CInt(pdfFileSize))
pdfFileStream.Close()
Response.ContentType = "application/pdf"
Response.OutputStream.Write(Buffer, 0, pdfFileSize)
Response.Flush()
Response.Close()
End Sub
I'm trying to restrict anonymous users from browsing directly to a particular filename (image file) in a folder on my website. But when I turn on the "IIS Authentication" feature on the folder, both anonymous users and the website application can't access the image file.
How can I deny access to the file for anonymous users (for example, if the user were to type in the absolute url), but allow access to the website application? (I thought that maybe the "IP Address and Domain Restrictions" feature could be used, as well, but couldn't get it to work)
I could move the image file to a folder outside of the website, but then not sure how to use it in the .ImageUrl property.
...bump
EDIT - Solution (put following in a .aspx page and set the ImageUrl property to this page, with any needed querystring parameters):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("FileType") IsNot Nothing) And (Request.QueryString("FileName") IsNot Nothing) Then
Try
' Read the file and convert it to Byte Array
Dim filePath As String = UrlXlat(Request.QueryString("FileType") & "\")
Dim fileName As String = Request.QueryString("FileName")
Dim contentType As String = "image/" & Path.GetExtension(fileName).Replace(".", "")
Dim fs As FileStream = New FileStream(filePath & fileName, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
br.Close()
fs.Close()
'Write the file to Reponse
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = contentType
Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
Catch ex As Exception
response.write(ex):response.end
End Try
End If
End Sub
You can move the file either to a folder outside of the root of the site or to the App_Data folder (which is protected form direct browsing by the ASP.NET framework) and then set your ImageUrl to point to a generic handler (.ashx file) which will be responsible for delivering the file to the browser. You perform your authentication checks in the handler.
I've written an article that provides implementation details: http://www.mikesdotnetting.com/article/122/simple-file-download-protection-with-asp-net
I'm currently trying to open a PDF file on my website that is located on my company's network. I had this working previously, but now for some reason it isn't working. Here is what I have:
I am using impersonation to access the file. It has domain admin privileges. This is from my web.config file (username/password are altered):
<identity impersonate="true" password="pass" userName="domain\user" />
I use this code to open the PDF in a window:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim strPath As String = CStr(Session("DWGPath"))
strPath = "file://san01/prodeng/" + Mid(strPath, 4)
strPath = Replace(strPath, "\", "/")
Dim pdfPath As String = strPath
Dim client As WebClient = New WebClient()
Dim buffer As Byte() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.End()
Catch exa As WebException
Response.Redirect("DrawingError.aspx")
Catch ex As Exception
Throw ex
End Try
End Sub
This doesn't work. It redirects me to the "DrawingError.aspx" page. This link dispalys the "Session("DWGPath")" variable. I can take this variable and paste it in to my browser and the PDF opens without problem.
However, if I alter my code to this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim strPath As String = CStr(Session("DWGPath"))
Dim pdfPath As String = strPath
Dim client As WebClient = New WebClient()
Dim buffer As Byte() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.End()
Catch exa As WebException
Response.Redirect("DrawingError.aspx")
Catch ex As Exception
Throw ex
End Try
End Sub
It still doesn't work.
The account also has full control privileges to the folder that contains the PDFs.
Any help or insight would be appreciated. Thank you!
EDIT: IF I throw exa then I get this:
The account used is a computer account. Use your global user account or local user account to access this server.
I assume you're running IIS. Go to the application pool for this app and change the identity that it's running under to be the domain\user account. See if that fixes your problem.
You want to make sure that the password on this account doesn't change or else it will fail when the password expires.
I want to consume my blog's RSS feed from other asp.net site. I can't get rss data. I try different methods (like HttpWebRequest) for consuming RSS feed but I always get same error.
WebException was caught.
The underlying connection was closed: An unexpected error occurred during an import operation.
What was the problem ?
feed address: http://blog.melihmucuk.com/feed/
I need post title, link and posted date.
for example :
Try
Dim reader As XmlTextReader = New XmlTextReader("http://blog.melihmucuk.com/feed/")
Dim ds As DataSet = New DataSet()
ds.ReadXml(reader) // incorrect line
Catch ex As Exception
End Try
I think, that's a simple task, but I don't know what is the problem.
Also I try this:
Try
Dim title As String
Dim link As String
Dim description As String
Dim reader = XmlReader.Create("http://blog.melihmucuk.com/feed/")//incorrect line
Dim feed = SyndicationFeed.Load(reader)
For Each item In feed.Items
title = item.Title.Text
link = item.Links(0).Uri.ToString
Next
HyperLink1.Text = title
HyperLink1.NavigateUrl = link
Label1.Text = description
Catch ex As Exception
End Try
Try this out:-
Imports System.Web
Imports System.Net
Imports System.IO
Public Class Reader
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim rssFeed = New Uri("http://blog.melihmucuk.com/feed/")
Dim request As WebRequest = WebRequest.Create(rssFeed)
Dim response As WebResponse = request.GetResponse()
Using reader As New StreamReader(response.GetResponseStream())
Dim xdoc As XDocument = New XDocument()
xdoc = XDocument.Load(reader)
'Read the nodes and display as per your requirement.
End Using
Catch ex As Exception
End Try
End Sub
End Class
Also add this block in your web.config file:-
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="true"/>
</defaultProxy>
</system.net>
</configuration>
When I open a page from another web application on same domain I am trying to read the cookie that was created on the original page, But it I cant read the cookie. If I move that page in to the same web application then it works, but thats not what I want.
[Domain: MyCompany.mobi]
[WebApp A] - create cookie and launch page
Protected Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim aCookie As New HttpCookie("TestCookie")
aCookie.Value = "Hello World"
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
Dim script = "window.open('http://MyCompany.mobi/webappB/default.aspx?id=TestCookie')"
'open page in WebsiteB
ScriptManager.RegisterStartupScript(Me, Me.GetType, "OpenPage", script, True)
End Sub
[Domain: MyCompany.mobi]
[WebApp B] - read the cookie and display in label
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim id As String = ""
If Request.QueryString("id") IsNot Nothing Then id = Request.QueryString("id").ToString
If Request.Cookies(id) IsNot Nothing Then
Dim aCookie As HttpCookie = Request.Cookies(id)
Label1.Text = aCookie.Name & " : " & aCookie.Value
Else
Label1.Text = "Cannot read cookie"
End If
End Sub
Cookies are associated with domain names; not the physical servers that are pointed to by DNS.
For security reasons, you cannot read cookies from a different domain name.
You need to pass the information in the URL.