2014-04-22 4 views
10

से फ़ाइल एक्सटेंशन प्राप्त करने का कोई तरीका है, मैं यह जानना चाहता हूं कि मेरी स्क्रिप्ट से डाउनलोड की जाने वाली फ़ाइल में एक्सटेंशन होगा जो मैं चाहता हूं।क्या यूआरएल

फ़ाइल की तरह यूआरएल पर नहीं होगा:

http://example.com/this_url_will_download_a_file 

या हो सकता है हाँ, लेकिन, मुझे लगता है कि मैं केवल यूआरएल उस तरह का उपयोग करेगा:

http://example.com/file.jpg 

मैं इसे जांच नहीं करेगा के साथ: Url.Substring(Url.LastIndexOf(".") - 3, 3) क्योंकि यह एक बहुत ही खराब तरीका है।

तो, आप मुझे क्या करने की सलाह देते हैं?

+0

आप की अंतिम स्थिति प्राप्त करने की कोशिश कर सकता है?। यदि पाया जाता है, तो अंतिम स्थिति पाएं। इससे पहले और बीच में सबकुछ वापस कर दें। यदि नही ? पाया जाता है, जो भी आखिरी स्थिति के बाद आता है। आपका फाइल एक्सटेंशन होगा। – Crono

+0

सबस्ट्रिंग को काम करना चाहिए, बस सुनिश्चित करें कि आप एक्सटेंशन के लिए खाते 3 से अधिक लंबाई के साथ खाते हैं। – Neolisk

+0

क्या आप यूआरएल प्रदान कर रहे हैं? क्या वे आपकी साइट या तृतीय पक्ष साइट पर हैं? –

उत्तर

6

यह अजीब है, लेकिन यह काम करता है:

string url = @"http://example.com/file.jpg"; 
string ext = System.IO.Path.GetExtension(url); 
MessageBox.Show(this, ext); 

लेकिन जैसा कि CRONO bellow टिप्पणी की, यह मानकों के साथ काम करेंगे नहीं:

string url = @"http://example.com/file.jpg?par=x"; 
string ext = System.IO.Path.GetExtension(url); 
MessageBox.Show(this, ext); 

परिणाम: ".jpg बराबर = एक्स"

+0

यह parametrized प्रश्नों के साथ काम नहीं करेगा। – Crono

+0

हाँ, क्रोनो सही है, मैंने इसे नहीं देखा :) – heringer

3

यदि आप .jpghttp://example.com/file.jpg का हिस्सा प्राप्त करना चाहते हैं तो Path.GetExtensionheringer सुझावों के साथ बस उपयोग करें।

// The following evaluates to ".jpg" 
Path.GetExtension("http://example.com/file.jpg") 

डाउनलोड लिंक http://example.com/this_url_will_download_a_file की तरह कुछ तो फ़ाइल नाम Content-Disposition का हिस्सा है, एक HTTP हेडर कि ब्राउज़र के लिए एक फ़ाइल नाम सुझाने के लिए प्रयोग किया जाता है के रूप में शामिल किया जाएगा कि एक "फ़ाइल सहेजें" संवाद प्रदर्शित है। आप इस फ़ाइल का नाम प्राप्त करना चाहते हैं तो आप डाउनलोड चालू करने की तकनीक Get filename without Content-Disposition ने सुझाव दिया उपयोग करें और HTTP हेडर प्राप्त कर सकते हैं, लेकिन वास्तव में फ़ाइल के किसी भी डाउनलोड करने

HttpWebResponse res = (HttpWebResponse)request.GetResponse(); 
using (Stream rstream = res.GetResponseStream()) 
{ 
    string fileName = res.Headers["Content-Disposition"] != null ? 
     res.Headers["Content-Disposition"].Replace("attachment; filename=", "").Replace("\"", "") : 
     res.Headers["Location"] != null ? Path.GetFileName(res.Headers["Location"]) : 
     Path.GetFileName(url).Contains('?') || Path.GetFileName(url).Contains('=') ? 
     Path.GetFileName(res.ResponseUri.ToString()) : defaultFileName; 
} 
res.Close(); 
0

मुझे पता है कि यह एक है बिना डाउनलोड को रद्द पुराना सवाल है, लेकिन यह सवाल देखने वाले लोगों के लिए सहायक हो सकता है।

यूआरएल के अंदर फ़ाइल नाम से विस्तार प्राप्त करने का सबसे अच्छा तरीका, पैरामीटर के साथ रेगेक्स के साथ भी हैं।

आप (न केवल यूआरएल) इस पद्धति का उपयोग कर सकते हैं:

.+(\.\w{3})\?*.* 

स्पष्टीकरण:

.+  Match any character between one and infinite 
(...) With this you create a group, after you can use for getting string inside the brackets 
\.  Match the character '.' 
\w  Matches any word character equal to [a-zA-Z0-9_] 
\?* Match the character '?' between zero and infinite 
.*  Match any character between zero and infinite 

उदाहरण:

http://example.com/file.png 
http://example.com/file.png?foo=10 

But if you have an url like this: 

http://example.com/asd 
This take '.com' as extension. 

तो तुम एक मजबूत पैटर्न की तरह यूआरएल के लिए उपयोग कर सकते हैं यह:

.+\/{2}.+\/{1}.+(\.\w+)\?*.* 

स्पष्टीकरण:

.+  Match any character between one and infinite 
\/{2}  Match two '/' characters 
.+  Match any character between one and infinite 
\/{1}  Match one '/' character 
.+  Match any character between one and infinite 
(\.\w+) Group and match '.' character and any word character equal to [a-zA-Z0-9_] from one to infinite 
\?*  Match the character '?' between zero and infinite 
.*  Match any character between zero and infinite 

उदाहरण:

http://example.com/file.png   (Match .png) 
https://example.com/file.png?foo=10 (Match .png) 
http://example.com/asd    (No match) 
C:\Foo\file.png      (No match, only urls!) 

http://example.com/file.png 

    http:  .+ 
    //   \/{2} 
    example.com .+ 
    /   \/{1} 
    file   .+ 
    .png   (\.\w+) 

अलविदा

संबंधित मुद्दे