0

私は、ユーザーが別のWebサイトにログインし、そのサーバーから指定されたファイルをダウンロードできるようにするこのアプリケーションに取り組んでいます。これまでのところ、Webサイトにログオンしてファイルをダウンロードすることに成功しました。しかし、zipファイルに関してはすべてが台無しになります。

バイトごとに、またはストリームリーダーを使用して.zipファイルを読み取るのに役立つ可能性のあるコードのチャンクはありますか?

使用してdownloadfile()いますが、正しいzipファイルが返されません。

zipファイルを読み取る方法が必要です。を使ってできますかByteReader()

zipファイルのダウンロードに使用されるコードは

string filename = "13572_BranchInformationReport_2012-05-22.zip";
            string filepath = "C:\\Documents and Settings\\user\\Desktop\\" + filename.ToString();
            WebClient client = new WebClient();
            string user = "abcd", pass = "password";
            client.Credentials = new NetworkCredential(user, pass);
            client.Encoding = System.Text.Encoding.UTF8;
            try
            {
                client.DownloadFile("https://web.site/archive/13572_BranchInformationReport_2012-05-22.zip", filepath);
                Response.Write("Success");
            }
            catch (Exception ue)
            {
                Response.Write(ue.Message);
            }

前もって感謝します。

4

3 に答える 3

4

ストリームリーダーを使用してzipファイルをバイト単位で読み取るのに役立つ可能性のあるコードのチャンクはありますか?

絶対違う。StreamReader-実際、バイナリコンテンツではなく、テキストTextReaderコンテンツを読み取るためのものです。zipファイルはテキストではなく、文字ではなくバイトで構成されています。

zipファイルなどのバイナリコンテンツを読んでいる場合は、あらゆる種類のコンテンツStreamではなく、を使用する必要があります。TextReader

WebClient.DownloadFileWebClient.DownloadDataは一般的に、バイナリコンテンツのダウンロードを容易にすることに注意してください。

于 2012-06-26T09:40:10.410 に答える
0

zipファイルをダウンロードするもう1つの簡単な方法

 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/DOWNLOAD/Filename.zip">Click To Download</asp:HyperLink>

別の解決策

 private void DownloadFile()
    {


        string getPath = "DOWNLOAD/FileName.zip";
        System.IO.Stream iStream = null;

        byte[] buffer = new Byte[1024];

        // Length of the file:
        int length;

        // Total bytes to read:
        long dataToRead;

        // Identify the file to download including its path.
        string filepath = Server.MapPath(getPath);

        // Identify the file name.
        string filename = System.IO.Path.GetFileName(filepath);
        try
        {
            // Open the file.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read, System.IO.FileShare.Read);


            // Total bytes to read:
            dataToRead = iStream.Length;
            // Page.Response.ContentType = "application/vnd.android.package-archive";
            //   Page.Response.ContentType = "application/octet-stream";
            Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Response.IsClientConnected)
                {
                    // Read the data in buffer.
                    length = iStream.Read(buffer, 0, 1024);

                    // Write the data to the current output stream.
                    Page.Response.OutputStream.Write(buffer, 0, length);

                    // Flush the data to the HTML output.
                    Page.Response.Flush();

                    //  buffer = new Byte[1024];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }
        }
        catch (Exception ex)
        {
            // Trap the error, if any.
            Page.Response.Write(ex.Message);
        }
        finally
        {
            if (iStream != null)
            {
                //Close the file.
                iStream.Close();
                Page.Response.Close();
            }

        }
    }
于 2012-06-26T11:45:07.770 に答える
0

あなたの答え

WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
byte[] buffer = new byte[32768];
using (Stream input = objResponse.GetResponseStream())
{
using (FileStream output = new FileStream ("test.doc",
FileMode.CreateNew))
{
int bytesRead;

while ( (bytesRead=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
} 

これが私がそれを達成した方法です。みんな助けてくれてありがとう

于 2012-06-27T05:29:27.303 に答える