c# - How to get valid file name and extension from remote url to save it.? -


I want to get the actual file extension from the remote URL.

Occasionally the extension is not in valid format

For example, I'm having trouble from the bottom of the URL

  1) /tctechcrunch2011.files.wordpress.com/2011/09/media-upload Png? W = 266 2) http://0.gravatar.com/avatar/a5a5ed70fa7c651aa5ec9ca8de57a4b8?s=60&d=identicon&r=G   

I want to download / save remotely < / P>

thanks = "post-text" itemprop = "text">

sends a content type header with a mime type of remote server resource. For example:

  Content-Type: image / png   

so that you can check the value of this header and select the appropriate extension for your file Able to For example:

  WebRequest request = WebRequest.Create ("http://0.gravatar.com/avatar/a5a5ed70fa7c651aa5ec9ca8de57a4b8?s=60&d=identicon&r=G"); (WebResponse response = request.GetResponse ()) (stream stream = response. Gateresonsstream ()) {string contentType = response.ContentType; // TODO: Check the content type and decide how to name your file string file - "test.jpg"; // File download using the file (stream file = file.OpenWrite (filename)) {// NOTE: If the file is too large, then it should be loaded into memory by memory / it is a memory byte [] buffer = new Avoid loading in byte [length of response content]; Stream Reader (buffer, 0, buffer lang); File. Write (buffer, 0, buffer lang); }}    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -