ASP.NET: Open File From URL - asp.net

In ASP.NET, I have an XML file (within my project) that I would like to deserialize. FileStream objects do not allow you to open a file via URL.
What is the easiest way to open the file so that I can deserialize it?

If the file is within your project you just need to get the physical location of the file. You can use Server.MapPath("/yourfile.txt") to get the phyical location and then open it with a filestream.

you can use the XmlDocument.Load(url) method which will load an XML Document from a URL

Related

Insert Image from Symfony path with PHPExcel

I'm trying to insert images in my Excel using the PHPExcel_Worksheet_Drawing of PHPExcel (in this Symfony Bundle) but I get this message all the time:
File http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg not found!
Here is my code:
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($path);
$objDrawing->setWidth(30);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($phpExcelObject->getActiveSheet());
My images are uploaded by VichUploaderBundle, and I'm generating the absolute path through a function. which returns me http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg
In a browser, this image is displayed!
Any clue?
Answer is based on my comments to this question.
There is a difference between URI and file path:
URI - used in browser, or being more general in HTTP protocol to get access to some resource via web-server
File path - a link to some file within filesystem on your server/computer/etc.
Your function returns a URI string (http://benefest.local/uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg), which can be used to access the image via a web server.
But PHPExcel_Worksheet_Drawing as well as other libraries use PHP's filesystem functions to get access to some file locally. So you need to get a local file path, instead of URI (in you case it is ./uploads/persons/image/58af244d3f58c_16143190_10210682923503464_8658615512523719175_n.jpg).

File upload control in MVC - making it look for file in original location

I am using a File Upload control in MVC project.
<input type="file" name="file" id="Ids2" style="float:right"/>
I get the following error when I try to save the file (TestDocument.txt) in the database. Lets say I click on the upload button and then browser to location - "c:\TestDocument.txt" - and try to upload it, I get this error..
Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\TestDocument.txt'.
Edit:(I am passing filename from the input type "file" value)
session.Advanced.DatabaseCommands.PutAttachment(id,null,
File.ReadAllBytes(#fileName), optionalMetaData);
Even though I select the text file from C:\ location, it always look for the file in the above "C:\Program Files (x86)\Co....." location. How do I make it take the file from its original location. Thanks for the help.
You can't just pass the filename of the file to the server and expect to open the file using that filepath. The path passed is on the clients local machine, but you are trying to open the file on the server.
You need a form element to post the actual file contents to the server.
Phil Haack has a good article with information on how to upload a file using MVC.
http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

Browse for file window without uploading the file

Is there a way to get the select file dialog box open and putting the location of the file into a textbox without ever uploading the file?
ETA I'm using VB.NET in a web page. By using the asp:fileupload tag I can get the file location
_fudFileLocation.PostedFile.FileName_
But how do I prevent the file from being uploaded at all. We don't need it, just the file location. (The files are on a shared drive so if it's M:\documents\todayslunch.pdf for person A, it's the same for person B.)
You do not get access to the full filepath in the browser because of security.
If this was possible, one could get the full layout of the computer of anyone going to any website.
System.Web.HttpPostedFile.FileName gets the fully qualified name of the file on the client which includes the directory path.

File attachment using virtual path in asp.net

I'm working on an asp.net project which has a form with a file attachment capability. Since I'm uploading the form on a server, I can't use a physical path. How do I use the asp:FileUpload with a virtual path?
You can convert a virtual path to a physical path:
HttpContext.Current.Request.MapPath("~/Example.txt")
I'm not sure you have this quite right. The file upload control will POST the file data to your server, and it can be accessed using the PostedFile property of the control.
You then save this file (I assume) to a physical path on your server (which you can access).

ASP.NET AdRotator AdvertisementFile xml file from outside the application

I have an ASP.NET web site, let's call it MySite, and at the same level as the web site, a virtual folder - Data. And in that folder I have the xml file needed in an AdRotator control.
I put "http://localhost/Data/Ads.xml" in AdvertisementFile and I get this error:
'http://localhost/Data/Ads.xml' is not a valid virtual path.
Is there a way to get this working?
The xml file must be on the same website for security reasons, using the control out of the box.
You could write your own methods to read a file from an external server, with an HttpWebRequest for example, then create a server side XML file from that stream and use it with the AdControl.
There is also the AdCreated event to look at, too.

Resources