我刚刚在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行上给出了null异常。
答案 0 :(得分:1)
你为什么要使用新的uri
byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload))); // null exception
这是一个直接来自msdn
的例子WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);