C# 通过OneDrive SDK上载文件时的文件路径

C# 通过OneDrive SDK上载文件时的文件路径,c#,file-upload,microsoft-graph-api,onedrive,microsoft-graph-sdks,C#,File Upload,Microsoft Graph Api,Onedrive,Microsoft Graph Sdks,使用OneDrive SDK上载文件 此时,您必须传递文件路径,但使用代码上载需要很长时间 即使通过临时文件路径,我也可以上载文件吗 public async Task<JObject> UploadLargeFiles(string upn, IFormFile files) { var jObject = new JObject(); int fileSize = Convert.ToInt32(files.Length);

使用OneDrive SDK上载文件

此时,您必须传递文件路径,但使用代码上载需要很长时间

即使通过临时文件路径,我也可以上载文件吗

public async Task<JObject> UploadLargeFiles(string upn, IFormFile files)
    {
        var jObject = new JObject();
        int fileSize = Convert.ToInt32(files.Length);

        var folderName = Path.Combine("wwwroot", "saveLargeFiles");
        var pathToSave = Path.Combine(System.IO.Directory.GetCurrentDirectory(), folderName);
        var fullPath = "";

        if (files.Length > 0)
        {
            var fileName = files.FileName;
            fullPath = Path.Combine(pathToSave, fileName);

            using (var stream = new FileStream(fullPath, FileMode.Create))
                files.CopyTo(stream);
        }

        var filePath = fullPath;

        var fileStream = System.IO.File.OpenRead(filePath);
        GraphServiceClient client = await MicrosoftGraphClient.GetGraphServiceClient();

        var uploadProps = new DriveItemUploadableProperties
        {
            ODataType = null,
            AdditionalData = new Dictionary<string, object>
            {
                { "@microsoft.graph.conflictBehavior", "rename" }
            }
        };

        var item = this.SelectUploadFolderID(upn).Result;
        var uploadSession = await client.Users[upn].Drive.Items[item].ItemWithPath(files.FileName).CreateUploadSession(uploadProps).Request().PostAsync();

        int maxChunkSize = 320 * 1024;
        var uploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxChunkSize);

        var response = await uploadTask.UploadAsync();

        if (response.UploadSucceeded)
        {
            return 
        }
        else
        {
            return null;
        }
    }
当前,我在将文件保存到服务器后获取文件路径

在这种情况下,速度问题会引发问题

有没有办法查看临时文件路径

public async Task<JObject> UploadLargeFiles(string upn, IFormFile files)
    {
        var jObject = new JObject();
        int fileSize = Convert.ToInt32(files.Length);

        var folderName = Path.Combine("wwwroot", "saveLargeFiles");
        var pathToSave = Path.Combine(System.IO.Directory.GetCurrentDirectory(), folderName);
        var fullPath = "";

        if (files.Length > 0)
        {
            var fileName = files.FileName;
            fullPath = Path.Combine(pathToSave, fileName);

            using (var stream = new FileStream(fullPath, FileMode.Create))
                files.CopyTo(stream);
        }

        var filePath = fullPath;

        var fileStream = System.IO.File.OpenRead(filePath);
        GraphServiceClient client = await MicrosoftGraphClient.GetGraphServiceClient();

        var uploadProps = new DriveItemUploadableProperties
        {
            ODataType = null,
            AdditionalData = new Dictionary<string, object>
            {
                { "@microsoft.graph.conflictBehavior", "rename" }
            }
        };

        var item = this.SelectUploadFolderID(upn).Result;
        var uploadSession = await client.Users[upn].Drive.Items[item].ItemWithPath(files.FileName).CreateUploadSession(uploadProps).Request().PostAsync();

        int maxChunkSize = 320 * 1024;
        var uploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, fileStream, maxChunkSize);

        var response = await uploadTask.UploadAsync();

        if (response.UploadSucceeded)
        {
            return 
        }
        else
        {
            return null;
        }
    }
public异步任务上传大文件(字符串upn,格式文件)
{
var jObject=new jObject();
int fileSize=Convert.ToInt32(files.Length);
var folderName=Path.Combine(“wwwroot”、“savelagefiles”);
var pathToSave=Path.Combine(System.IO.Directory.GetCurrentDirectory(),folderName);
var fullPath=“”;
如果(files.Length>0)
{
var fileName=files.fileName;
fullPath=Path.Combine(路径保存,文件名);
使用(var stream=newfilestream(fullPath,FileMode.Create))
文件.CopyTo(流);
}
var filePath=fullPath;
var fileStream=System.IO.File.OpenRead(文件路径);
GraphServiceClient=等待MicrosoftGraphClient.GetGraphServiceClient();
var uploadProps=新的DriveItemUploadableProperties
{
ODataType=null,
AdditionalData=新字典
{
{“@microsoft.graph.conflictBehavior”,“重命名”}
}
};
var item=this.SelectUploadFolderID(upn).Result;
var uploadSession=wait client.Users[upn].Drive.Items[item].ItemWithPath(files.FileName).CreateUploadSession(uploadProps.Request().PostAsync();
int maxChunkSize=320*1024;
var uploadTask=new LargeFileUploadTask(uploadSession、fileStream、maxChunkSize);
var response=await uploadTask.UploadAsync();
if(response.uploadsuccessed)
{
返回
}
其他的
{
返回null;
}
}

服务器的磁盘可能不是导致此速度变慢的原因。默认情况下,上传的文件存储在一个临时目录中,您可以像您一样使用
CopyTo(FileStream)
永久保存该目录

您可以跳过此步骤并调用
ifformfile.OpenReadStream()
获取临时文件的流,然后将其传递给
LargeFileUploadTask

关键是,上传到OneDrive可能需要花费最多的时间。根据您的设置,您可能希望将文件保存到队列目录(请求完成后临时文件将被删除),并让后台服务读取该队列并将其上载到OneDrive