I have an application that reads an XML file for information on projects and displays them in a timelines. The user has the ability to modify and add projects, so I want to save this XML file.
I have a Silverlight application that displays the data, and a web project that hosts the XML file in it's ClientBin folder. The application gets the XML file by using the WebClient class:
WebClient dataSource = new WebClient();
dataSource.OpenReadCompleted += dataSource_OpenReadCompleted;
dataSource.OpenReadAsync(new Uri("ProjectData.xml", UriKind.Relative));
Then in the dataSource_OpenReadCompleted method it gets the stream from the e.Result object and reads it into an XDocument object which I parse using LINQ. This works fine.
Now I want to save my changes back to the web project. I have the modified XML in an XDocument object ready to go ... and I'm not sure how to write back.
There is a WebClient.OpenWriteAsync method, but I'm not sure how to use it. Googling doesn't give any clear results.
Thanks,
Andrew
Silverlight code runs on the client so...
You could try making a webservice that accepts the xml data and a file name. Then have that web service write the file back to the file system. Also please ensure that you use locks on the file so that multiple users don't try to write to the file at the same time.
Thanks for the suggestion kurtnelle, that would also work. I figure I'll write what my solution was for anyone who stumbles upon this question.
One cannot use OpenWriteAsync the same way you use OpenReadAsync because for security reasons Silverlight cannot write directly to the file system.
I ended up using an HTTPHandler method in the web project. I created a file called XMLHandler.ashx that listened for a call to the webclient. In the Silverlight app I called webclient.UploadStringAsync with the URI of the web project and the xml file as the data string. When the HTTPHandler hears this, it saves it to the client bin. It works perfectly!
Related
This is my first time developing this kind of system, so many of these concepts are very new to me. Any and all help would be appreciated. I'll try to sum up what I'm doing as efficiently as possible.
Background: I have a web application running AngularJS with Bootstrap. The app communicates with the server and DB through a web service programmed using C#. On the site, users can upload files and reference them later using direct links. There's no restriction to file type (yet), so just about anything is allowed.
My Goal: Having direct links creates a big security problem for me, since the documents/images are supposed to be private data. What I would prefer to do is validate a user's credentials when the link is clicked, then load the file in the browser using a more generic url path.
--Example--
"mysite.com/attachments/1" ---> (Image)
--instead of--
"mysite.com/data/files/importantImg.jpg"
Where I'm At: Not very far. My first thought was to add a page that sends the server request and receives a file byte stream along with mime type that I can reassemble and present to the user. However, I have no idea if this is possible using a web service that sends JSON requests, nor do I have a clue about how the reassembling process would work client-side.
Like I said, I'll take any and all advice. I'd love to learn more about this subject for future projects as well, but for now I just need to be pointed in the right direction.
Your first thought is correct, for it, you need to use the Response object, and more specifically the AddHeader and Write functions. Of course this will be a different page that will only handle file downloads, so it will be perfectly fine in your JSON web service.
I don't think you want to do this with a web service. Just use a regular IHttpHandler to perform the validation and return the data. So you would have the URL "attachments/1" get rewritten to "attachments/download.ashx?id=1". When you've verified access, write the data to the response stream. You can use the Content Disposition header to set the file name.
I am trying to read the contents of a JSON file sitting in my github pages repository.
I can navigate and see the file contents in my browser if I specify the url.
If I use the code here:
http://www.codeproject.com/Tips/397574/Use-Csharp-to-get-JSON-Data-from-the-Web-and-Map-i?msg=4615047#xx4615047xx
It claims to "just work", but it doesn't.
All I get back is:
<html><frameset><frame src="URL-TO-JSON-FILE"></frameset></html>
How am I supposed to read the json file and get its contents back as a string. I am using c#?
Once I get the JSON string back I can do the processing I need to do in c#.
EDIT:
According to rawgithub.com those types of urls are not to be used for production. I need this for production. How do production website read remote JSON files that are located on a webserver?
Thank you
Sometimes in github, if you wish to use code from a repository, you must change the url to raw.github.com/ or click on the raw button and use this url.
I want to grab a set of data from a site into my C# application. I've referred to some sites and articles using the WebClient class.
But the problem is the data I want is in a news bar made using flash. Is it possible to grab the data from it? The data in it also keeps on updating as well.
Have you tried the Yahoo approach? The below project does just that.
It is easy to download stock data from Yahoo!. For example, copy and
paste this URL into your browser address:
http://download.finance.yahoo.com/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2.
Depending on your Internet browser setting, you may be asked to save
the results into a filename called "quotes.csv" or the following will
appear in your browser:
http://www.codeproject.com/KB/aspnet/StockQuote.aspx?display=Normal
It is unable to grab a data from Flash.
One possible solution is that, if you dig into embed tag at the Flash object or find some url or rss that looks to be consumed by the flash, you can read that by WebClient or (hopefully) XmlReader.
I have a C# client which once every hour needs to post some zip files to ASP.Net site. This needs to be completely automated with no user interaction.
Wondering the best way to go about it.
Ideally would like to post the file without setting up any non .aspx / .asp pages.
Thanks for the help!
It depends on what the target site expects as content type. If it is multipart/form-data then a simple WebClient should do the job:
using (var client = new WebClient())
{
byte[] result = client.UploadFile(
"http://foo.com/index.aspx", #"d:\foo\bar.zip"
);
// TODO: Handle the server response if necessary
}
Send a HttpRequest containing all the necessary information including the bytes of the file. Google should help you on this one.
Nevertheless, I don't understand why you don't want to use a non .aspx page for this. A generic handle (.ashx) is suitable for this. But I still suggest you use another way to upload that file, e.g. per FTP and use a service that watches the directoy with a FileWatcher to determine and act on changes
In order to automate the task, you can use a DispatcherTimer (http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx), assigning a handler to the Tick event.
This is a web service call which I wrote that is intended to receive a WAV file via a POST and store it in the web-app server's local file system (IIS). Is there a simple method to store the file and if so would someone be so kind as to provide a C# example?
You'll need to have write access to the directory you want to save to.
Make a FileUpload control, then call its SaveAs method in a postback.
If you're writing a REST service, use the following code:
Request.Files[0].SaveAs(/* some file path */);
Either way, be aware of the security issues - make sure the filename has a .wav extension and don't trust the file to be correct.