作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在使用以下方法将图片上传到 Google 云端硬盘:
public string AddFile(string path, string contentType, string driveId)
{
FilesResource.CreateMediaUpload request;
using (FileStream stream = new FileStream(path,
FileMode.Open))
{
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File
{
Name = Path.GetFileName(path),
DriveId = driveId
};
request = _service.Files.Create(
fileMetadata, stream, contentType);
request.Fields = "id";
request.SupportsTeamDrives = true;
}
IUploadProgress requestResult = request.Upload();
if (requestResult.Exception != null) throw requestResult.Exception;
Google.Apis.Drive.v3.Data.File file = request.ResponseBody;
return file == null ? "" : file.Id;
}
使用 API 客户端库版本 1.40.3.1694,直到今天它都运行良好。现在我在 requestResult.Exception 中遇到这种错误:
System.FormatException: The format of value 'bytes 0--1/660915' is invalid.
我上传的文件只是一个小的 GIF 测试文件,在这种情况下会立即删除。
我是否漏掉了一些明显的东西?
最佳答案
我明白了:
MemoryStream
修复它我还不明白:
问题是您正在打开一个 FileStream
,创建一个请求但不执行它,处理 FileStream
和 然后 执行请求。在您的 MemoryStream
版本中,您不会在上传完成之前处理流。
创建初始请求实际上并不从流中读取数据——它只是准备一些东西。在 Upload
完成之前,您需要保持流打开。因此,您需要做的就是使 using
语句变大:
public string AddFile(string path, string contentType, string driveId)
{
using (FileStream stream = new FileStream(path, FileMode.Open))
{
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File
{
Name = Path.GetFileName(path),
DriveId = driveId
};
var request = _service.Files.Create(fileMetadata, stream, contentType);
request.Fields = "id";
request.SupportsTeamDrives = true;
IUploadProgress requestResult = request.Upload();
if (requestResult.Exception != null) throw requestResult.Exception;
Google.Apis.Drive.v3.Data.File file = request.ResponseBody;
return file == null ? "" : file.Id;
}
}
关于c# - Google Drive API 上传格式无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57796896/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!