2013-03-14 7 views
6

के माध्यम से एक फ़ाइल डाउनलोड करें मेरे पास एक फॉर्म पर वेबब्रोसर नियंत्रण है, लेकिन अधिकांश भाग के लिए यह उपयोगकर्ता से छिपा रहता है। यह लॉगिन और अन्य कार्यों की एक श्रृंखला को संभालने के लिए है। I को इस नियंत्रण को का उपयोग करना है क्योंकि लॉगिन का एक टन है जो लॉगिन को संभालता है। (यानी, मैं सिर्फ वेब क्लाइंट ऑब्जेक्ट पर स्विच नहीं कर सकता।)वेबब्रोसर नियंत्रण

थोड़ा सा होप करने के बाद, हम एक पीडीएफ फ़ाइल डाउनलोड करना चाहते हैं। लेकिन डाउनलोड करने के बजाय, फ़ाइल वेब ब्राउजर नियंत्रण में प्रदर्शित होती है, जिसे उपयोगकर्ता नहीं देख सकता है।

ब्राउज़र नियंत्रण में लोड होने के बजाय मैं पीडीएफ कैसे डाउनलोड कर सकता हूं?

+1

भविष्य में संदर्भ के लिए एक देर से जवाब। [URLDownloadToFile] (http://msdn.microsoft.com/en-us/library/ms775123 (v = vs.85) .aspx) API [इसका उपयोग इस के लिए किया जा सकता है] (http://stackoverflow.com/a/ 19043430/1768303)। – Noseratio

उत्तर

11

अपने फ़ॉर्म के एक SaveFileDialog नियंत्रण जोड़ें, फिर अपने WebBrowser के नेविगेट घटना पर निम्नलिखित कोड जोड़ें:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) 
{ 
    if (e.Url.Segments[e.Url.Segments.Length - 1].EndsWith(".pdf")) 
    { 
     e.Cancel = true; 
     string filepath = null; 

     saveFileDialog1.FileName = e.Url.Segments[e.Url.Segments.Length - 1]; 
     if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      filepath = saveFileDialog1.FileName; 
      WebClient client = new WebClient(); 
      client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); 
      client.DownloadFileAsync(e.Url, filepath); 
     } 
    } 
} 

// कॉलबैक समारोह

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
{ 
    MessageBox.Show("File downloaded"); 
} 

स्रोत: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d338a2c8-96df-4cb0-b8be-c5fbdd7c9202

+2

यह एक महान समाधान है। यह बिल्कुल ठीक नहीं हुआ जो मुझे चाहिए, लेकिन यह वास्तव में अच्छा है। धन्यवाद हेनलेट। – Jerry

5

समाधान मैं इसका उपयोग कर समाप्त हुआ:

मैंने सब कुछ किया यूआरएल प्राप्त करने के लिए जरूरी है जहां इसे जाने की जरूरत है। यह जानकर कि कुकीज़ में सभी लॉगिन जानकारी, आवश्यक सेटिंग्स, व्यूस्टेट इत्यादि संग्रहीत किए गए थे, अंततः वेब क्लाइंट के हाइब्रिड का उपयोग करके फ़ाइल को पकड़ने में सक्षम था, फिर वेब क्लाइंट ऑब्जेक्ट को वास्तव में फ़ाइल बाइट्स को स्नैग करने के लिए नेविगेट करने में सक्षम था।

public byte[] GetPDF(string keyValue) 
    { 
     DoLogin(); 

     // Ask the source to generate the PDF. The PDF doesn't 
     // exist on the server until you have visited this page 
     // at least ONCE. The PDF exists for five minutes after 
     // the visit, so you have to snag it pretty quick. 
     LoadUrl(string.Format(
      "https://www.theMagicSource.com/getimage.do?&key={0}&imageoutputformat=PDF", 
      keyValue)); 

     // Now that we're logged in (not shown here), and 
     // (hopefully) at the right location, snag the cookies. 
     // We can use them to download the PDF directly. 
     string cookies = GetCookies(); 

     byte[] fileBytes = null; 
     try 
     { 
      // We are fully logged in, and by now, the PDF should 
      // be generated. GO GET IT! 
      WebClient wc = new WebClient(); 
      wc.Headers.Add("Cookie: " + cookies); 

      string tmpFile = Path.GetTempFileName(); 

      wc.DownloadFile(string.Format(
       "https://www.theMagicSource.com/document?id={0}_final.PDF", 
       keyValue), tmpFile); 

      fileBytes = File.ReadAllBytes(tmpFile); 
      File.Delete(tmpFile); 
     } 
     catch (Exception ex) 
     { 
      // If we can't get the PDF here, then just ignore the error and return null. 
      throw new WebScrapePDFException(
       "Could not find the specified file.", ex); 
     } 

     return fileBytes; 
    } 

    private void LoadUrl(string url) 
    { 
     InternalBrowser.Navigate(url); 

     // Let the browser control do what it needs to do to start 
     // processing the page. 
     Thread.Sleep(100); 

     // If EITHER we can't continue OR 
     // the web browser has not been idle for 10 consecutive seconds yet, 
     // then wait some more. 
     // ... 
     // ... Some stuff here to make sure the page is fully loaded and ready. 
     // ... Removed to reduce complexity, but you get the idea. 
     // ... 
    } 

    private string GetCookies() 
    { 
     if (InternalBrowser.InvokeRequired) 
     { 
      return (string)InternalBrowser.Invoke(new Func<string>(() => GetCookies())); 
     } 
     else 
     { 
      return InternalBrowser.Document.Cookie; 
     } 
    } 
1
bool documentCompleted = false; 
    string getInnerText(string url) 
    { 
     documentCompleted = false; 
     web.Navigate(url); 

     while (!documentCompleted)   
      Application.DoEvents(); 


     return web.Document.Body.InnerText; 
    } 
    private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     documentCompleted = true; 
    } 
संबंधित मुद्दे