直接网络传输C#(Direct network transfers C#)

编程入门 行业动态 更新时间:2024-10-28 09:21:17
直接网络传输C#(Direct network transfers C#)

我只是好奇,是否可以在c#中进行直接网络传输,而无需本地缓存。

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

在那个妈妈我可以将文件下载到本地电脑,然后将其上传到谷歌硬盘。 但是可以直接从一个谷歌驱动器上传到另一个谷歌驱动器,或者至少在完全下载完成之前开始上传。

谢谢

I was just curious, is it possible to have direct network transfers in c#, without local caching.

e.g. I have response stream which represents GoogleDrive file and request stream to upload file to another GoogleDrive account.

At that momment I can download file to local pc and next upload it to the google drive. But is it possible to upload it directly from one google drive to another or, at least, start uploading before full download will be completed.

Thank

最满意答案

是的,您可以使用Google Drive api将文件下载到流中,然后将其保存在内存中,以便在登录后将其上传到其他Google云端硬盘帐户。

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

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

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

文件下载示例:

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; }

文件插入示例:

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; } }

Yes you can, with Google Drive api you download file into a stream and you keep it in memory so you can upload it to another google drive account after login.

You get your token on first account and download a file keeping it in a stream.

THen you authenticate on other google drive account and upload the file using the stream.

PS: When you are inserting the file on the second drive account, instead of getting the byte[] array reading the file from disk you get the byte array from the stream you have in memory.

File Download Example:

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; }

File insert example:

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; } }

更多推荐

本文发布于:2023-08-04 14:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网络传输   Direct   transfers   network

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!