【问题标题】:WebClient to download data from deployed webWebClient 从已部署的 Web 下载数据
【发布时间】:2013-09-06 01:26:47
【问题描述】:

我刚刚在 IIS 上部署了我的网站,在我的 WPF 端我想从中下载数据,所以我就是这样做的:

   public StartWindow()
    {
        InitializeComponent();
        label11.Width = Double.NaN;
        stackPanelTitle.Width = Double.NaN;
        DownloadData("http://localhost/iStellarMobile_deploy/Puzzle/CrossTest.txt"); // null exception 

    }

    protected void DownloadData(string strFileUrlToDownload)
    {
        byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload))); // null exception 

        MemoryStream storeStream = new MemoryStream();

        storeStream.SetLength(myDataBuffer.Length);
        storeStream.Write(myDataBuffer, 0, (int)storeStream.Length);

        storeStream.Flush();

        using (FileStream file = new FileStream(@"C:\TestFile.txt", FileMode.Create, System.IO.FileAccess.Write))
        {
            byte[] bytes = new byte[storeStream.Length];
            storeStream.Read(bytes, 0, (int)storeStream.Length);
            file.Write(bytes, 0, bytes.Length);
            storeStream.Close();
        }

        //TO save into certain file must exist on Local
       //storeStream.SaveAs(storeStream,  @"C:\TestFile.txt");

        //The below Getstring method to get data in raw format and manipulate it as per requirement
        string download = Encoding.ASCII.GetString(myDataBuffer);


    }

这是正确的方法吗?我指的是http://www.c-sharpcorner.com/UploadFile/97fc7a/reading-files-from-given-specific-url-using-webclient/

它在我评论的 2 行上给了我空异常。

【问题讨论】:

    标签: c# asp.net wpf web-applications webclient


    【解决方案1】:

    你为什么使用新的 uri

     byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload))); // null exception
    

    这是一个直接取自 msdn 的示例

    WebClient client = new WebClient();
    byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);
    

    http://msdn.microsoft.com/en-us/library/xz398a3f.aspx

    【讨论】:

    • 您好,谢谢回复,我试过你的,它说访问被拒绝。我尝试为它创建目录,但它仍然显示拒绝访问。
    • 您可能会授予该文件夹的 iis 权限
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多