首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >直接网络传输C#

直接网络传输C#
EN

Stack Overflow用户
提问于 2012-12-04 05:04:46
回答 1查看 420关注 0票数 0

我只是好奇,有没有可能在c#中有直接的网络传输,而不需要本地缓存。

例如,我有表示GoogleDrive文件的响应流和将文件上传到另一个GoogleDrive帐户的请求流。

在那一刻,我可以下载文件到本地pc,然后上传到google驱动器。但是有没有可能直接从一个google驱动器上传到另一个驱动器,或者至少在完全下载完成之前开始上传。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-04 05:07:42

可以,使用Google Drive api,您可以将文件下载到流中,并将其保存在内存中,以便登录后可以将其上传到另一个google drive帐户。

您在第一个帐户上获得令牌,并下载一个文件,将其保存在流中。

THen你在其他谷歌驱动器帐户上进行身份验证,并使用流上传文件。

PS:当您在第二个驱动器帐户上插入文件时,不是让byte[]数组从磁盘读取文件,而是从内存中的流中获取字节数组。

文件下载示例:

代码语言:javascript
复制
public static System.IO.Stream DownloadFile(
      IAuthenticator authenticator, File file) {
    if (!String.IsNullOrEmpty(file.DownloadUrl)) {
      try {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
            new Uri(file.DownloadUrl));
        authenticator.ApplyAuthenticationToRequest(request);
        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK) {
          return response.GetResponseStream();
        } else {
          Console.WriteLine(
              "An error occurred: " + response.StatusDescription);
          return null;
        }
      } catch (Exception e) {
        Console.WriteLine("An error occurred: " + e.Message);
        return null;
      }
    } else {
      // The file doesn't have any content stored on Drive.
      return null;
    }

文件插入示例:

代码语言:javascript
复制
private static File insertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename) {
    // File's metadata.
    File body = new File();
    body.Title = title;
    body.Description = description;
    body.MimeType = mimeType;

    // Set the parent folder.
    if (!String.IsNullOrEmpty(parentId)) {
      body.Parents = new List<ParentReference>()
          {new ParentReference() {Id = parentId}};
    }

    // File's content.
    byte[] byteArray = System.IO.File.ReadAllBytes(filename);
    MemoryStream stream = new MemoryStream(byteArray);

    try {
      FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
      request.Upload();

      File file = request.ResponseBody;

      // Uncomment the following line to print the File ID.
      // Console.WriteLine("File ID: " + file.Id);

      return file;
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
      return null;
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13691683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档