c# - 如何在 Xamarin 表单中将图像文件上传到 GoogleDrive
问题描述
我想知道是否有任何方法可以以 Xamarin 形式将图像从应用程序上传到 Google Drive,我使用此链接关注 Google Drive API
https://developers.google.com/drive/api/v3/manage-uploads
但在 xamarin 这种方式不起作用某些库不可用
这是我的代码
var image = new Image
{
Source = ImageSource.FromFile(filepath)
};
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = "photo.jpg"
};
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(filepath,
System.IO.FileMode.Open))
{
request = DriveService.Files.Create(
fileMetadata, stream, "image/jpeg");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);
DriveService 显示编译错误,我不知道这种方式是否可行,有什么帮助吗?
解决方案
Google Drive API 在 Xamarin.iOS 和 Xamarin.Android 中可用。因此您可以DependencyService
在 iOS 和 Android 平台上使用和上传图像。
在表单中,创建界面
public interface IUploadFile
{
void UploadFile(string filePath,string fileName);
}
iOS 实现
[assembly: Dependency(typeof(UploadFileImplementation))]
namespace xxx.iOS
{
public class UploadFileImplementation : IUploadFile
{
public UploadFileImplementation () { }
public UploadFile(string filePath,string fileName)
{
// upload file here
}
}
}
安卓实现
[assembly: Dependency(typeof(UploadFileImplementation))]
namespace xxx.Droid
{
public class UploadFileImplementation : IUploadFile
{
public UploadFileImplementation () { }
public UploadFile(string filePath,string fileName)
{
// upload file here
}
}
}
您可以从 nuget下载适用于iOS和Android的Google Drive API。它提供了如何在本机平台上上传文件的示例。
推荐阅读
- excel - 达克斯。根据另一列的最大值获取一列的值
- c# - HttpClient 在 IIS 上的 ASP.NET Core 上返回错误 404,但适用于 IIS Express
- amazon-web-services - 外部用户的 Redshift?
- javascript - 如何使用 Vue.js 删除嵌套的 Firebase 数据
- angular - 无效的元素状态:元素必须是用户可编辑的,以清除它尝试单击并使用 Selenium 在下拉切换中插入日期的错误
- svg - SVG 异物内的文本不可见
- python - Pandas - 在保留索引的同时将列拆分为行
- swift - 是否可以从 Any 类型中获取他的 UnsafeRawPointer 和长度?
- javascript - 在父组件内使用组件的多个实例
- python - Python pandas 将两个数据集的两列转换为一列,保留列名和数据类型