不能使用REST API从SkyDrive中下载完整映像文件 [英] Can't download complete image file from skydrive using REST API
问题描述
我正在快速包装在C#中的SkyDrive的API,但运行与下载文件的问题。对于文件的第一部分,一切都经过精细,但随后有开始是文件中的差异不久之后一切都变得无效。我相当肯定,这只是我无法正确读取数据流。
I'm working on a quick wrapper for the skydrive API in C#, but running into issues with downloading a file. For the first part of the file, everything comes through fine, but then there start to be differences in the file and shortly thereafter everything becomes null. I'm fairly sure that it's just me not reading the stream correctly.
这是我使用的下载文件中的代码:
This is the code I'm using to download the file:
public const string ApiVersion = "v5.0";
public const string BaseUrl = "https://apis.live.net/" + ApiVersion + "/";
public SkyDriveFile DownloadFile(SkyDriveFile file)
{
string uri = BaseUrl + file.ID + "/content";
byte[] contents = GetResponse(uri);
file.Contents = contents;
return file;
}
public byte[] GetResponse(string url)
{
checkToken();
Uri requestUri = new Uri(url + "?access_token=" + HttpUtility.UrlEncode(token.AccessToken));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = WebRequestMethods.Http.Get;
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
byte[] contents = new byte[response.ContentLength];
responseStream.Read(contents, 0, (int)response.ContentLength);
return contents;
}
这是我试图下载的图像文件
This is the image file I'm trying to download
这是我收到的图像
这两个图像使我相信,我不会等待响应完成未来通过的,因为内容长度是一样的图像的大小我期待的,但我不知道如何让我的代码等待整个响应才能通过,甚至真的,如果这就是我需要采取的方法。
These two images lead me to believe that I'm not waiting for the response to finish coming through, because the content-length is the same as the size of the image I'm expecting, but I'm not sure how to make my code wait for the entire response to come through or even really if that's the approach I need to take.
下面是我的测试代码的情况下,这是有帮助的。
Here's my test code in case it's helpful
[TestMethod]
public void CanUploadAndDownloadFile()
{
var api = GetApi();
SkyDriveFolder folder = api.CreateFolder(null, "TestFolder", "Test Folder");
SkyDriveFile file = api.UploadFile(folder, TestImageFile, "TestImage.png");
file = api.DownloadFile(file);
api.DeleteFolder(folder);
byte[] contents = new byte[new FileInfo(TestImageFile).Length];
using (FileStream fstream = new FileStream(TestImageFile, FileMode.Open))
{
fstream.Read(contents, 0, contents.Length);
}
using (FileStream fstream = new FileStream(TestImageFile + "2", FileMode.CreateNew))
{
fstream.Write(file.Contents, 0, file.Contents.Length);
}
Assert.AreEqual(contents.Length, file.Contents.Length);
bool sameData = true;
for (int i = 0; i < contents.Length && sameData; i++)
{
sameData = contents[i] == file.Contents[i];
}
Assert.IsTrue(sameData);
}
它未能在 Assert.IsTrue(sameData);
推荐答案
这是因为你不检查的返回值responseStream.Read(内容,0,(INT)response.ContentLength);
。 读
并不能保证它会读 response.ContentLength
字节。相反,它返回读的字节数。您可以使用一个循环或 stream.CopyTo
有
This is because you don't check the return value of responseStream.Read(contents, 0, (int)response.ContentLength);
. Read
doesn't ensure that it will read response.ContentLength
bytes. Instead it returns the number of bytes read. You can use a loop or stream.CopyTo
there.
事情是这样的:
WebResponse response = request.GetResponse();
MemoryStream m = new MemoryStream();
response.GetResponseStream().CopyTo(m);
byte[] contents = m.ToArray();
这篇关于不能使用REST API从SkyDrive中下载完整映像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!