Save a WAV file to disk - c#

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.

Related

How can we prevent files that have changed the extension from being uploaded?

I have a program that allow to user upload a file.
Suppose the user is only allowed to upload the psd file (or any other file that is specified by admin user). If user changes the file extension from .exe to psd, can upload it.
How can we prevent files that have changed the extension from being uploaded?
You could check for ContentType header, but that could be tampered by someone with enough knowledge to do so.
Like MrTux pointed out, the best way would be to read the file header and check if it's complying to the file format you want to check against.
It would be easy to decide if you have information on how users are currently uploading files. If it's your own system or if users compose their own http post requests.

What should I be saving locally when I use Azure blob storage?

I'm using Azure Blob Storage to allow users to upload files from a web app.
I've got them uploading into a container, but I'm not sure what would be best to save on the web app's database since there are multiple options.
There is a GUID for the file, but also a URL.
The URL can be used to get to the file directly, but is there a risk that it could change?
If I store the file GUID I can use that to get the other details of the file using an API, but of course that's and extra step compared to the URL.
I'm wondering what best practices are. Do you just store the URL and be done with it? Do you store the GUID and always make an extra call whenever a page loads to get the current URL? Do you store both? Is the URL something constant that can act just as good as a GUID?
Any suggestions are appreciated!
If you upload any file on azure blob it will give you Url to access it which contains three part
{blob base url}/{Container Name}/{File Name}
e.g
https://storagesamples.blob.core.windows.net/sample-container/logfile.txt
SO you can save Blob base url and container name in config file and only the file name part in data base.
and at run time you can create whole url and return it back to user.
So in case if you are changing blob or container you just need to change it in config file.

Problem saving XML File to web project in Silverlight

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!

Accessing http://< someserver.com/logs:<someportnumber> > in client side using c# code

I have a scenario in my mind .. I need validations/suggestions from Stack over flow !! :)
There is a (remote)apache server hosting this URL "http://someserver.com/logs/log.txt:4041" .When i hit this URL in IE it opens a page containing log.txt in a file-folder-directory structure (after authentication).
Is there any way to get the attributes of the log.txt (attributes what i mean is file creation date,file modification date,file size etc..).
What I am planning to do is to write a code in C#.net(in the client) using system.IO namespace and using the fileinfo class and use
FileInfo fi = new FileInfo(pathname);
fi.CreationTime.toString() to retrieve the file creation time.
This is successful for files that exists in local directories in my hard drive!! .
Is it possible to use the same code for retrieving the information about the file that exists in the server that is accessed using the URL "http://someserver.com/logs/log.txt:4041" ?? if yes should i give the URL in my pathename ?
Take it for granted that i have access to the server by authentication..
You can try to inspect the data in WebResponse.Headers. The web server will send some date/time information of the file with the response. This may however not be what you expect depending on the web server you're calling and whether the page you are loading is a script or a file on disk. Settings of the web server will also influence the details returned.
You will not be able to use FileInfo method for items retrieved from http. However if the directory that the file that you are getting served to you is accessible from a share of from samba you could use this method.

Web File Properties

Is it possible to get file properties of a web file. ie: PDF file and get the date and time the PDF file was modified. Thanks.
You're probably looking for the HttpWebResponse.LastModified property.
No, you can't. It's not a file, it's a resource.
When you request it from the server you get a response back. In this case the server uses a file as source for the resource, but it could just as well be something that is just created on the fly. You only get the information that the server chooses to put in the HTML header.
You might however be able to get some more information if the server returns directory information. Then you could request the folder and get a listing back, from which you could parse some information. Note however that it's not certain that the server actually uses the files when you request something from the directory, it could still return something completely different. Also, the directory listing is also a resource, so there is no guarantee that it's relevant either.

Categories

Resources