2009-07-06 11 views
21

क्या HTTP का उपयोग कर स्थानीय निर्देशिका में फ़ाइल डाउनलोड करने के लिए कोई अंतर्निहित तरीका है?मैं फ़ाइल डाउनलोड करने के लिए msbuild का उपयोग कैसे कर सकता हूं?

मैं एक कस्टम कार्य को भूलने या लिखने के लिए खोल सकता हूं, लेकिन मैं यह सुनिश्चित करना चाहता था कि इसे पूरा करने का कोई मौजूदा तरीका न हो।

अग्रिम धन्यवाद!

उत्तर

1

एमएसबिल्ड सामुदायिक कार्य परियोजना में वेबडाउनलोड कार्य के अलावा, एमएसबिल्ड एक्सटेंशन पैक (वर्तमान संस्करण: 4.x) में WebClient कक्षा है जिसका उपयोग फ़ाइल डाउनलोड करने के लिए किया जा सकता है। आप MSBuild विस्तार पैक यहाँ डाउनलोड कर सकते हैं:

<Project ToolsVersion="4.0" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
<PropertyGroup> 
    <TPath>$(MSBuildProjectDirectory)\..\MSBuild.ExtensionPack.tasks</TPath> 
    <TPath Condition="Exists('$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks</TPath> 
</PropertyGroup> 
<Import Project="$(TPath)"/> 
<Target Name="Default"> 
    <!-- Download a File--> 
    <MSBuild.ExtensionPack.Web.WebClient TaskAction="DownloadFile" Url="http://hlstiw.bay.livefilestore.com/y1p7GhsJWeF4ig_Yb-8QXeA1bL0nY_MdOGaRQ3opRZS0YVvfshMfoZYe_cb1wSzPhx4nL_yidkG8Ji9msjRcTt0ew/Team%20Build%202008%20DeskSheet%202.0.pdf?download" FileName="C:\TFS Build 2008 DeskSheet.pdf"/> 
    <!-- Get the contents of a Url--> 
    <MSBuild.ExtensionPack.Web.WebClient TaskAction="OpenRead" Url="http://www.msbuildextensionpack.com"> 
     <Output TaskParameter="Data" PropertyName="Out"/> 
    </MSBuild.ExtensionPack.Web.WebClient> 
    <Message Text="$(Out)"/> 
</Target> 

के रूप में:

यहाँ एक फ़ाइल डाउनलोड करने के लिए MSBuild एक्सटेंशन पैक 4 का उपयोग करने का एक उदाहरण है एक अलग उत्तर में उल्लिखित, WebClient में एक सुरक्षित से पासवर्ड डाउनलोड करने की क्षमता नहीं है (पासवर्ड सुरक्षा एड) वेब सर्वर।

10

यदि आप ऐसी फ़ाइल डाउनलोड करने का प्रयास कर रहे हैं जिसके लिए प्रमाणीकरण की आवश्यकता है (जैसे कि टीएफएस वेब, या किसी डोमेन से जुड़े आईआईएस सर्वर), न तो एमएसबिल्ड एक्सटेंशन पैक और न ही एमएसबिल्ड कम्युनिटी टास्क में एक देने की क्षमता है HTTP सर्वर पर उपयोगकर्ता नाम या पासवर्ड। इस मामले में, मैंने कस्टम एमएसबिल्ड कार्य लिखना समाप्त कर दिया। मैंने जो किया है वह यहाँ है।

मैंने Download a file which requires authentication using vb.net/c#? के उत्तर में स्टैक ओवरफ़्लो उपयोगकर्ता डौग की सलाह का पालन किया, जिसमें उन्होंने कोड गुरु वेब साइट पर टॉम आर्चर द्वारा लिखी गई विधि में जोड़ने के लिए कुछ कोड सुझाए।

तो मैं निम्न कोड एक MSBuild लक्ष्य Wget नामित बनाने के लिए के साथ एक नई सी # प्रोजेक्ट बनाने के लिए एमएस दृश्य स्टूडियो 2010 का इस्तेमाल किया (पूर्ण स्रोत कोड दिखाया गया है):

// Include references to the following frameworks in your solution: 
// - Microsoft.Build.Framework 
// - Microsoft.Build.Utilities.v4.0 
// - System 
// - System.Net 

using System; 
using System.Net; 
using System.IO; 

using Microsoft.Build.Framework; 
using Microsoft.Build.Utilities; 

namespace Wget 
{ 
    public class Wget: Task 
    { 
     [Required] 
     public String Address // HTTP address to access 
     { get; set; } 

     [Required] 
     public String LocalFilename // Local file to which the downloaded page will be saved 
     { get; set; } 

     public String Username // Credential for HTTP authentication 
     { get; set; } 

     public String Password // Credential for HTTP authentication 
     { get; set; } 

     public override bool Execute() 
     { 
      int read = DownloadFile(Address, LocalFilename, Username, Password); 

      Console.WriteLine("{0} bytes written", read); 

      return true; 
     } 

     public static int DownloadFile(String remoteFilename, String localFilename, String httpUsername, String httpPassword) 
     { 
      // Function will return the number of bytes processed 
      // to the caller. Initialize to 0 here. 
      int bytesProcessed = 0; 

      // Assign values to these objects here so that they can 
      // be referenced in the finally block 
      Stream remoteStream = null; 
      Stream localStream = null; 
      WebResponse response = null; 

      // Use a try/catch/finally block as both the WebRequest and Stream 
      // classes throw exceptions upon error 
      try 
      { 
       // Create a request for the specified remote file name 
       WebRequest request = WebRequest.Create(remoteFilename); 
       if (request != null) 
       { 
        // If a username or password have been given, use them 
        if (httpUsername.Length > 0 || httpPassword.Length > 0) 
        { 
         string username = httpUsername; 
         string password = httpPassword; 
         request.Credentials = new System.Net.NetworkCredential(username, password); 
        } 

        // Send the request to the server and retrieve the 
        // WebResponse object 
        response = request.GetResponse(); 
        if (response != null) 
        { 
         // Once the WebResponse object has been retrieved, 
         // get the stream object associated with the response's data 
         remoteStream = response.GetResponseStream(); 

         // Create the local file 
         localStream = File.Create(localFilename); 

         // Allocate a 1k buffer 
         byte[] buffer = new byte[1024]; 
         int bytesRead; 

         // Simple do/while loop to read from stream until 
         // no bytes are returned 
         do 
         { 
          // Read data (up to 1k) from the stream 
          bytesRead = remoteStream.Read(buffer, 0, buffer.Length); 

          // Write the data to the local file 
          localStream.Write(buffer, 0, bytesRead); 

          // Increment total bytes processed 
          bytesProcessed += bytesRead; 
         } while (bytesRead > 0); 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
      } 
      finally 
      { 
       // Close the response and streams objects here 
       // to make sure they're closed even if an exception 
       // is thrown at some point 
       if (response != null) response.Close(); 
       if (remoteStream != null) remoteStream.Close(); 
       if (localStream != null) localStream.Close(); 
      } 

      // Return total bytes processed to caller. 
      return bytesProcessed; 
     } 
    } 
} 
उस जगह में साथ

, मैं जोड़ सकते हैं मेरी MSBuild परियोजना के लिए निम्न कार्य:

<!-- Get the contents of a Url--> 
<Wget 
    Address="http://mywebserver.com/securepage" 
    LocalFilename="mydownloadedfile.html" 
    Username="myusername" 
    Password="mypassword"> 
</Wget> 

Wget कार्य पेज mywebserver.com द्वारा सेवा डाउनलोड करता है और mydownloadedfile.html के रूप में वर्तमान कार्यशील निर्देशिका में एक फ़ाइल में बचत होती है, उपयोगकर्ता नाम "myusername" और पासवर्ड का उपयोग "मेरा पासवर्ड"।

हालांकि, कस्टम Wget MSBuild कार्य का उपयोग करने के लिए, मुझे MSBuild को Wget असेंबली फ़ाइल (.dll) कहां मिलनी चाहिए। इस MSBuild के तत्व के साथ किया जाता है:

<!-- Import your custom MSBuild task --> 
<UsingTask AssemblyFile="MyCustomMSBuildTasks\Wget\bin\Release\Wget.dll" TaskName="Wget" /> 

आप कल्पना प्राप्त करना चाहते हैं, तो आप भी अपने MSBuild परियोजना Wget निर्माण से पहले यह कहा जाता है हो सकता है। ऐसा करने के लिए, <MSBuild Projects> कार्य के साथ समाधान का निर्माण, और <UsingTaks AssemblyFile> कार्य के साथ यह आयात करते हैं, कुछ इस तरह:

<!-- Build the custom MSBuild target solution--> 
<MSBuild Projects="MyCustomMSBuildTasks\CustomBuildTasks.sln" Properties="Configuration=Release" /> 

<!-- Import your custom MSBuild task --> 
<UsingTask AssemblyFile="MyCustomMSBuildTasks\Wget\bin\Release\Wget.dll" TaskName="Wget" /> 

<!-- Get the contents of a Url--> 
<Wget 
    Address="http://mywebserver.com/securepage" 
    LocalFilename="mydownloadedfile.html" 
    Username="myusername" 
    Password="mypassword"> 
</Wget> 

तो आप एक कस्टम MSBuild लक्ष्य पहले कभी नहीं बनाया गया है, यह भी मुश्किल नहीं है - एक बार आप मूल बातें जानते हैं।ऊपर सी # कोड देखें, आधिकारिक एमएसडीएन दस्तावेज पर नज़र डालें, और अधिक उदाहरणों के लिए वेब पर चारों ओर खोजें। एक अच्छी जगह शुरू करने के लिए है:

+0

समुदायटास्क का वेबडाउनलोड अब प्रमाणीकरण का समर्थन करता है। – rasjani

25

MSBuild 4.0 में आप संकलन और एक अलग विधानसभा में एक कस्टम कार्य को तैनात करने की आवश्यकता होगी, से बचने के लिए इनलाइन कार्यों का उपयोग कर सकते हैं।

<UsingTask TaskName="DownloadFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> 
    <ParameterGroup> 
     <Address ParameterType="System.String" Required="true"/> 
     <FileName ParameterType="System.String" Required="true" /> 
    </ParameterGroup> 
    <Task> 
     <Reference Include="System" /> 
     <Code Type="Fragment" Language="cs"> 
     <![CDATA[ 
      new System.Net.WebClient().DownloadFile(Address, FileName); 
     ]]> 
     </Code> 
    </Task> 
    </UsingTask> 

    <Target Name="DownloadSomething"> 
    <DownloadFile Address="http://somewebsite/remotefile" FileName="localfilepath" /> 
    </Target> 
+1

जब मैंने पहली बार यह पढ़ा तो यह स्पष्ट नहीं था कि दो तत्व कहां रखना है। मैंने को से ऊपर जोड़ा है जिसमें मेरी प्रोजेक्ट फाइलें हैं और मैंने में रखा है। –

+0

अनजाने में यह एमएसबिल्ड के डॉटनेट कोर संस्करण के साथ काम नहीं करता है। – sakra

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

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