首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从.NET上载文件到一个驱动器

如何从.NET上载文件到一个驱动器
EN

Stack Overflow用户
提问于 2018-08-05 02:35:07
回答 1查看 10.6K关注 0票数 4

我需要维护某些文档的历史记录,我最初的解决方案是将它们从.NET复制到一个共享文件夹中,但这对我来说并不安全。我是否可以使用带有C#的.NET将这些文件上载到一个驱动器?如果是这样的话,我想要关于它的文档,我已经做了一个免费的搜索,但我还没有找到任何能满足我需求的东西。如果这个问题太模糊了,我道歉。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-05 03:08:45

也许这能帮到你:

使用以下代码:

代码语言:javascript
复制
public string OneDriveApiRoot { get; set; } = "https://api.onedrive.com/v1.0/";

将文件上传到OneDrive

代码语言:javascript
复制
//this is main method of upload file to OneDrive 
public async Task<string> UploadFileAsync(string filePath, string oneDrivePath) 
{ 
    //get the upload session,we can use this session to upload file resume from break point 
    string uploadUri = await GetUploadSession(oneDrivePath); 

    //when file upload is not finish, the result is upload progress, 
    //When file upload is finish, the result is the file information. 
    string result = string.Empty; 
    using (FileStream stream = File.OpenRead(filePath)) 
    { 
        long position = 0; 
        long totalLength = stream.Length; 
        int length = 10 * 1024 * 1024; 

        //In one time, we just upload a part of file 
        //When all file part is uploaded, break out in this loop 
        while (true) 
        { 
        //Read a file part 
        byte[] bytes = await ReadFileFragmentAsync(stream, position, length); 

        //check if arrive file end, when yes, break out with this loop 
        if (position >= totalLength) 
        { 
            break; 
        } 

        //Upload the file part 
        result = await UploadFileFragmentAsync(bytes, uploadUri, position, totalLength); 

        position += bytes.Length; 
    } 
} 

return result; 
} 

private async Task<string> GetUploadSession(string oneDriveFilePath) 
{ 
    var uploadSession = await AuthRequestToStringAsync( 
    uri: $"{OneDriveApiRoot}drive/root:/{oneDriveFilePath}:/upload.createSession", 
    httpMethod: HTTPMethod.Post, 
    contentType: "application/x-www-form-urlencoded"); 

JObject jo = JObject.Parse(uploadSession); 

return jo.SelectToken("uploadUrl").Value<string>(); 
} 

private async Task<string> UploadFileFragmentAsync(byte[] datas, string uploadUri, long position, long totalLength) 
{ 
    var request = await InitAuthRequest(uploadUri, HTTPMethod.Put, datas, null); 
    request.Request.Headers.Add("Content-Range", $"bytes {position}-{position + datas.Length - 1}/{totalLength}"); 

return await request.GetResponseStringAsync(); 
} 

获取共享链接:( Javascript )

代码语言:javascript
复制
//This method use to get ShareLink, you can use the link in web or client terminal 
public async Task<string> GetShareLinkAsync(string fileID, OneDriveShareLinkType type, OneDrevShareScopeType scope) 
{ 
string param = "{type:'" + type + "',scope:'" + scope + "'}"; 

string result = await AuthRequestToStringAsync( 
    uri: $"{OneDriveApiRoot}drive/items/{fileID}/action.createLink", 
    httpMethod: HTTPMethod.Post, 
    data: Encoding.UTF8.GetBytes(param), 
    contentType: "application/json"); 

return JObject.Parse(result).SelectToken("link.webUrl").Value<string>(); 
} 

来自:https://code.msdn.microsoft.com/office/How-to-upload-file-to-21125137

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51688525

复制
相关文章

相似问题

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