文件未按预期在 Asp.Net 核心控制器中传输
File not streaming as expected in Asp.Net Core controller
我正在尝试将文件上传到我的 onedrive,按预期方式获取文件似乎没有进行相应的处理,任何人都可以指出问题所在。
代码片段:
public async Task<IActionResult> UploadFileOnFolder(IFormFile uploadedFile, string folderName)
{
try
{
if (uploadedFile.Length > 0)
{
dynamic response;
var fileName = ContentDispositionHeaderValue.Parse(uploadedFile.FileName).FileName.Trim('"');
using var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(fileName));
response = await _graphServiceClient.Me.Drive.Items[folderName].Content
.Request()
.PutAsync<DriveItem>(stream);
ViewData["FileUploadInfo"] = response;
}
else
{
ViewData["message"] = "Sorry Upload failed! Please check your file!";
}
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
}
return RedirectToAction("RecentDirveInfo");
}
注:我在关注this official document.
我不确定 APi,但我知道您正在尝试上传文件名作为文件。您可能希望使用 uploadedFile.OpenReadStream
而不是将其传递给 PutAsync
Opens the request stream for reading the uploaded file.
using var stream = uploadedFile.OpenReadStream();
response = await _graphServiceClient.Me.Drive.Items[folderName].Content
.Request()
.PutAsync<DriveItem>(stream);
试试下面的代码:
public async Task<IActionResult> UploadFileOnFolder(IFormFile uploadedFile, string folderName)
{
try
{
if (uploadedFile.Length > 0)
{
dynamic response;
var fileName = uploadedFile.FileName;
using var stream = uploadedFile.OpenReadStream();
response = await _graphServiceClient.Me.Drive.Root.ItemWithPath(folderName +"/" + fileName).Content.Request().PutAsync<DriveItem>(stream);
ViewData["FileUploadInfo"] = response;
}
else
{
ViewData["message"] = "Sorry check your file!";
}
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
}
return RedirectToAction("RecentDirveInfo");
}
我已经对它进行了测试,它非常适合我:
顺便说一句,在您 运行 此代码之前,请确保您已向您的 Azure AD 应用程序授予以下 委派权限 :
Files.ReadWrite, Files.ReadWrite.All, Sites.ReadWrite.All
我正在尝试将文件上传到我的 onedrive,按预期方式获取文件似乎没有进行相应的处理,任何人都可以指出问题所在。
代码片段:
public async Task<IActionResult> UploadFileOnFolder(IFormFile uploadedFile, string folderName)
{
try
{
if (uploadedFile.Length > 0)
{
dynamic response;
var fileName = ContentDispositionHeaderValue.Parse(uploadedFile.FileName).FileName.Trim('"');
using var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(fileName));
response = await _graphServiceClient.Me.Drive.Items[folderName].Content
.Request()
.PutAsync<DriveItem>(stream);
ViewData["FileUploadInfo"] = response;
}
else
{
ViewData["message"] = "Sorry Upload failed! Please check your file!";
}
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
}
return RedirectToAction("RecentDirveInfo");
}
注:我在关注this official document.
我不确定 APi,但我知道您正在尝试上传文件名作为文件。您可能希望使用 uploadedFile.OpenReadStream
而不是将其传递给 PutAsync
Opens the request stream for reading the uploaded file.
using var stream = uploadedFile.OpenReadStream();
response = await _graphServiceClient.Me.Drive.Items[folderName].Content
.Request()
.PutAsync<DriveItem>(stream);
试试下面的代码:
public async Task<IActionResult> UploadFileOnFolder(IFormFile uploadedFile, string folderName)
{
try
{
if (uploadedFile.Length > 0)
{
dynamic response;
var fileName = uploadedFile.FileName;
using var stream = uploadedFile.OpenReadStream();
response = await _graphServiceClient.Me.Drive.Root.ItemWithPath(folderName +"/" + fileName).Content.Request().PutAsync<DriveItem>(stream);
ViewData["FileUploadInfo"] = response;
}
else
{
ViewData["message"] = "Sorry check your file!";
}
}
catch (Exception ex)
{
ViewData["message"] = ex.Message;
}
return RedirectToAction("RecentDirveInfo");
}
我已经对它进行了测试,它非常适合我:
顺便说一句,在您 运行 此代码之前,请确保您已向您的 Azure AD 应用程序授予以下 委派权限 :
Files.ReadWrite, Files.ReadWrite.All, Sites.ReadWrite.All