(C#) download embedded flash from a given URL - c#

I have a desktop forms application . I want to fetch any flash file embedded from a given html page. I used this approach: parse the html page(using htmlagilitypack) to get hold of direct url of any embedded flash, then fetch the file. But this approach wouldn't work if relative paths are used.
How else can a flash file embedded in an html page be downloaded ?
ty

You already have the domain and the base path from URL you are fetching from. Surely Path.Combine can do the rest?

You could determine the absolute path.

Related

Image src pointing on a file outside my project folder in WEB FORM

I'm looking to do this exactly :
set src property in view to a url outside of the MVC3 project
Fine but in web form ?
I tried simply putting the path as a string into the src of the image :
<asp:Image ID="imgInside" runat="server" src="\\serverName.com\dfs$\APPL-ADM\FichiersDev\MandatsInfo\SAR220-2020_1.jpg" >
Obviously not working, so I made src pointe on this function I wrote like so :
<asp:Image runat="server" Width="160px" src='<%# getImage(Container.DataItem as MandatMobile.DAL.MandatsEcoleCC_Result) %>' ></asp:Image>
In back end C# :
protected Byte[] getImage(MandatsEcoleCC_Result p)
{
using (MandatsDatas db = new MandatsDatas())
{
GROUPE_ARTICLE g = db.GROUPE_ARTICLE.First(t => t.ID_GROUPE == p.ID_GROUPE);
if (string.IsNullOrWhiteSpace(g.image))
return null;
FileStream fs = null;
try
{
fs = new FileStream(#"\\serverName.com\dfs$\APPL-ADM\FichiersDev\MandatsInfo\" + g.MANDAT.NO_MANDAT + g.image, FileMode.Open, FileAccess.Read);
}
catch
{
}
BinaryReader br = new BinaryReader(fs);
return br.ReadBytes((int)fs.Length);
}
}
Still not working, I've been searching but I just can't figure it out and I'm stuck trying all sorts of non-sens.
Well, you confusing two things:
Code behind:
Anytime you run code that uses a file, then you writing 100% server side code. As such any file path is a proper windows FULL qualified path name. It has ZERO ZERO to do with web URL's.
Read the above a dozen times. Your code does not use URL path names - end of story.
Web site:
Anytime you reference a file, picture, script files or anything? You are and MUST use a URL based on the path names of the web site, and more so path names that resolve to the folders that represent the site.
root:
\Pictures (say a folder in the web site folder list with pictures.
So, a src, or ANY URL in the web site? They do NOT use windows path names like code behind.
So, if there is a cat.png picture in folder pictures? When your URL will be this:
www.mywebsite.com/Pictures/cat.png
If you write code to read/load/see/use that cat.png picture? Then you convert in code from that extenral URL to a full qualifed standard windows path name (with back slaches).
So, in code behind if you want to read, or do somthing with the above file?
You use
dim strFile as string
strFile = Server.MapPath("www.mywebsite.com/Pictures/cat.png")
map path will now return a full qualified windows server path
eg:
c:\inetpub\wwwrootmysite\Pictures\cat.png
Ok, so now we realize that to use a VALID link to pictures on teh web site, we MUST use a valid URL.
So, what happens if say we have a network connected HUGE massive say SAN drive or some other huge server on the network that has huge storage, and has our pictures in that site?
Say:
\SANSERVER\WebPictures\cat.png
Well, obviosity that file folder can't be used in a URL. ONLY URL's in the web folder path name can be used. And this is a good thing. Since when I go to www.amazon.com it is a VERY good thing I can't type in a URL to get at their intenral accouting files server and steal all the credit card information of all customers.
So, now, how can I get at that cat.png, and turn it into a valid URL?
There are two ways:
One:
You make the decision to expose and INCLUDE the above path name as part of the web site. This is typical done with what is called a virutal folder. You need IIS, and during development with IIS + Visual Studio, it is a "pain" to setup such path names. But if you have full version of IIS, then you can add the virutal folder to the web site though the IIS user interface tools.
So, you add a virutal folder called MyPictures, and it will be mapped to:
\SANSERVER\WebPictures\cat.png
So, now the web site URL becomes:
www.mycoolsite.com/MyPictures/cat.png
And in code if you do a server.map path, the above url will return this:
\SANSERVER\WebPictures\cat.png
Ok, next issue:
I don't want to expose that other folder to the web site. I don't want a valid URL, and I don't even want users to be able to type in say this:
www.mycoolsite.com/MyPictures/doggie.png
So, if you DO expose another folder or add a folder to the web site hiarchy, then users ARE FREE to type in a URL that will resolve to that other folder (but you are assumed to have added a virtual folder to the web site).
Now, with a valid URL resolution, then you can place markup code on teh web site, and provide valid full URL path names to the picture or whatever for the web site.
However, lets say for reason of security, I do NOT want that other server to be exposed to as a URL?
Well, it it is NOT exposed as a valid web URL folder, then you can NOT put in a valid URL - it that's simple.
However, that don't mean the code behind can't read/load/open that file on the server. In fact the web site code behind can often read any file on the server, and in fact read any file anyplace on the network that the web server is running. And as noted, code behind does not use URL
s, and does not use "forward" "/" for the file - but a plan jane old fashting fully qualfied windows path name.
Since the code behind can darn near read any file and do anything it wants?
Ok, then how can we get the code behind to dish out a file, or send that file to the web site?
Two simple ways:
Your code behind could read the cat.png file, and copy it to a folder that is part of the web server folder layout. Once one, then you can provide a valid URL. However, with a huge picture library, that would be pain full.
And in some cases the picture might come from a database row(s) that store pictures, and once again no valid path name exists for the web site.
So, what you can do is read the file in code behind and then "stream" the data directly to the web site.
When you steam contents from code behind, then you don't care nor even require a valid URL, because the code behind is pumping out the object data (in this case a picture cat.png) directly to the web browser. So this is often done because your pictures don't even exist in a file, or in fact it not practical to include that folder in the web site folder list for reasons of security.
As noted, if this was/is just a folder of pictures OUTSIDE of the folders for the web site? Well then 99% of the time, then adding a mapped folder (a virtual folder) to the web site that points to the picture hard drive is common done, and is practical.
however, you might have a HUGE library of pictures on a big file server, and you have a database that has key words for searching the pictures, and the database row stores a valid path name to the hard drive/server that has all the pictures in a Hodge podge folder hierarchy that is not practical to expose as web based urls.
So, how to stream a file?
You code is close, but you need to include additional information. And unfortantly the server can't stream the file down as 100% binary format.
So, say we drag + drop a image control onto the form. You have this:
<asp:Image ID="Image1" runat="server" />
So, now in code behind to stream + set the picture to a picture on the hard drive?
You can use this:
Dim strFile As String = "c:\Test4\pcards.bmp"
Me.Image1.ImageUrl = Gimage2(strFile)
Now of course the URL path name to the above Test4 folder does not exist.
Gimage2 - it just converts the file as a byte array, and then to a string coded as base64.
Function Gimage2(strPath As String) As String
Dim PicData As Byte() = Nothing
PicData = File.ReadAllBytes(strPath)
Dim ContentType As String = "image/" & Path.GetExtension(strPath)
Return "data:" & ContentType & ";base64," & Convert.ToBase64String(PicData, 0, PicData.Length)
End Function
So I spent some time with a long post. The reason is you attempted to use a URL with standard windows back slashes, and that means in your mind, you are using the concept of a windows full path name and MAJOR confusing that with a URL path name. Failure to make this distingishing will cause you years of pain and suffering. You must have BEYOND CRYSTAL clear this concpet of a URL and that of a file name in code behind. They are two VERY different things.
If that addtional folder is "ok" to expose to the web site? Then create a Virtural folder.
That means:
wwww.mycoolsite.com/MyPictures/dog.png
Could in fact point to ANY mapped folder on your server. And this means the web server will require permisions to that folder, and in most cases thus a user (or your code) can type in and use a full web path name to the picture.
However, as noted, for pdf documents and many other types of files, then it is out of the question to have a valid URL and a mapped folder. So you can use the 100% file based approach as per above, and read the file as bytes, and then stream + output the file to the browser.
You can even do a response.write and pump out the file directly to the browser, but then again you don't have much control as to where it will be. Do realize that pumping out a string as base 64 data as per above can and will cause some bloat and expansion in the size of the string sent to be rendered as a picture. So for a simple image - sure that's ok. But for a larger high quality high resolution image, then of course I don't recommend you send the picture as a base64 string due to the expansion that string will result in.
I ended up putting a fonction in another MVC project that works correctly to retrieve images.
So my src path point on an URL instead of a file on a server path.
src='https://NameOf_MVC_webSite.csdn.qc.ca/imageBank/ForMandat?name=' + (Container.DataItem as MandatMobile.DAL.MandatsEcoleCC_Result).image
Dirty solution using another deployed app that has a (better / easy to use / functional) framework
But this is not an "OK" solution

How to add an download link to users in my project

In my project i will be having an link like
Download
I want the users to download files of different types. The file will be in the root folder. When i am clicking on the link it is displaying an error. This is the plugin to install in the chrome. If the user download this link and open then it will automatically add to the chrome.
How can i do this.
The file is not even downloading.
This isn't a valid path:
~/hello world.crx
The ~ character is for use server-side to denote the root of the application. Client-side it has no meaning. The browser doesn't know what the root of the application is (or what the application is at all), it's just sending requests to resources at addresses. And it doesn't know what to do with that address.
You'll need to either use some server-side logic to translate that path into a browser-useable path, or manually make it a relative or absolute path.
If the ASP.NET MVC Framework isn't translating this for you then you're probably using a version that requires a little more manual work for it. Try something like:
Download
(Note: This assumes the use of the Razor view engine. If you're not using that then you'll want to use whatever your view engine equivalent is.)
What you need to do is set up a directory online, where you can host the file.
I also see that in your aref you don't want to type the full path so denote it with a /hello_world.crx, but make sure that you've set up a base href:
<base href="http://yourdomain.com/something/">
Try renaming the file to remove any spaces e.g. "hello_world.crx" and then change the name in the link code to match.
if a webpage and the downloadable file is in the same location
(i.e)
SampleFolder->Download.html
SampleFolder->hello world.crx
then try the below
download
If the webpage and the downloadable file in different location
(i.e)
SampleFolder->Download.html
SampleFolder->Downloads->hello world.crx
then try the below
download

How to load an image from the server in a web application in C#

I actually have a C# winform application which load images from my computer C://images/... with the Image object and the function Fromfile.
Image.FromFile(Path);
but in my web application (ASP)
<asp:Image ID="viewPhoto" runat="server" Width="550px" Height="400px"/>
I use the attribute ImageURL.
viewPhoto.ImageURL = Path
But the problem is that it doesn't find the correct path because with this way. The path will be http://localhost:3656/C://images....
I would like to load an image directly from my server to have the correct path for both of my applications.(web ASP and winform)
Image.FromFile(/images/myimage.jpg)
This actually doesn't work because the program doesn't find any photo in this path.
First of all i think that images you are trying to show are not in web application folder / virtual directory. Move images folder to your web application folder and then use:
Page.ResolveClientUrl("images/test.jpg");
or for server side:
Server.MapPath("images/test.jpg");
If you dont want to move images to your web folder then your only choice is to write HttpHandler which will read images from C:\images folder and transmit it to the client. This will also require some specific permissions for your web app IIS user to access some folder outside the web app scope.
You can see the sample of HttpHandler here: Thumbnailer HTTP Handler
ASP.Net image simply renders the ImageURL to the client browser, this will then try to load that image from the location specified, this typically needs to be a resource available via your website.
Try moving your images inside your web root then you can access them like :
viewPhoto.ImageUrl = "~/images/yourimage.jpg";
As im sure you know your <asp:Image> element is actually creating an html <img> element that will instruct the browser to request the image from the browser.
Image.FromFile will give you an image object on the server side but it won't be much use for your clients browser.
As #HABJAN said, you need to map the path from your local file to a relative web URI and to do that you can use Server.MapPath. However your images will need to be inside your asp.net project folder which is asccessible by the web server in order for it to serve the file.
If you really want to share the paths in some sort of shared constant assembly I suggest you make your windows application work with relative paths and duplicate the images folder structure for both the windows app and the web app.
I found the quickest way to resolve this was to assign the image path as a css attribute. The only downside of this is that if you are looking to display a large amount of images in a repeater you would not want to use this, but if you have one or two images you can use this.
<style type="text/css">
#errorImage{
background: url(/path/path/images/image.jpg);
background-size: contain;
height: 100px;
width: 100px;
}
</style>
<div id="errorImage"><div>
Using the path you are using now (http://localhost:3656/C://images....), the application will look for a /C://images... folder in your app directory on the web server. I believe you should create an images directory in your application folder and server the images from there so that you could reference the images using a relative path.

How do I read a file located in a same folder where my page resides in in ASP.NET?

How do I read a file located in a same folder where my page resides in in ASP.NET (C#)?
I have a page called mypage.aspx and I'm trying to read in a file called foo.txt residing in a same directory as this page.
Is there a way to open that file for reading with File.OpenRead()?
Providing a relative path like File.OpenRead("foo.txt") fails b/c of the location of the file.
It should be something like
File.OpenRead(Server.MapPath("foo.txt"));
You should try File.OpenRead(Server.MapPath("foo.txt")).
If MapPath doesn't expand/can't find the proper path at this point then try it while specifying the relative path to the page in question starting from the sites virtual root (using the tilde (~) at the beginning of the string to indicate this), i.e. File.OpenRead(Server.MapPath("~/path/foo.txt"))
In ASP.NET the folder is really IIS's folder which is typically in C:\Windows\System32\Inetsrv\ etc.
What you will need to do is use either
Server.MapPath("TheFileName").
Or get the PhysicalApplicationPath from the Request using
Request.PhysicalApplicationPath
or
HttpRuntime.AppDomainAppPath
and go from the Request and then go from there
You can use a label message or textbox in the aspx page and you can display the file in that by using the below code, I had used a label message wit lblDisplay ID.
lblDisplay.Text = File.ReadAllText(Server.MapPath("Give the path here"));

Loading a pdf document using absolute path

I have a requirement where i want to load pdf document in a web page from a physical path. The pdf document location is not inside my website directory. To elaborate on this with example: Let assume my virtual directory refers to "c:\website". I have all my pdf documents stored under different folder called c:\pdfDocuments". On one of my web page i want to load my pdf document from c:\pdfdocuments. Is there way to pass the absolute path in this case (c:\pdfdocuments\x.pdf) to frame control's src attribute.
Thanks
CS
No, you cannot do that unless the C:\pdfdocuments is also a website; and in that case you would need to pass in the URL that relates to that physical path.
Keep in mind that the frame, or other html element, is trying to load the contents of the file accross the internet from the browser to your server. The browser on the clients end has no knowledge nor access to your physical filesystem, only what is exposed via the web server.
Now, if you're trying to load this on the server side, then you should be able to use the physical path as long as the worker process has access permissions to that path. But based on the question ".. to frame control's src attribute." I'm assuming you're referring the the client side html.

Categories

Resources