【问题标题】:Upload File using Xamarin Filepicker plugin使用 Xamarin Filepicker 插件上传文件
【发布时间】:2018-03-27 05:49:48
【问题描述】:

我正在尝试使用 nuget 包文件选择器上传 pdf 文件。
我能够获取文件名和文件本地路径。 但是当我尝试将文件转换为字节数组时,我遇到了错误。

这是我的 pcl 代码-

public interface ILoclFileProvider
{
    byte[] GetFileBytes(string path);
}

public static class FileUtility
{
    public static ILoclFileProvider FileSystem { get; set; }

    public static void SetUp(ILoclFileProvider fs)
    {
        FileSystem = fs;
    }
}

这是我的 Droid 项目代码-

public class LocalFileProvider_Droid : ILoclFileProvider
{
    public byte[] GetFileBytes(string filePath)
    {
        return File.ReadAllBytes(filePath);
    }
}

在我的 pcl 项目中,我称之为-

var bytes = FileUtility.FileSystem.GetFileBytes(filePath);

我遇到了一个错误-

对象引用未设置为对象的实例。

我的代码有什么问题?

【问题讨论】:

  • 你需要使用DependencyService。

标签: c# xamarin.forms


【解决方案1】:

我认为您调用 DependencyService 是错误的。试试这个:

var bytes = DependencyService.Get<ILoclFileProvider>().GetFileBytes(filePath);

还要确保您通过 Android 项目中命名空间上方的正确属性注册它。

[assembly: Xamarin.Forms.Dependency (typeof (LocalFileProvider_Droid))]

此行确保在 Xamarin 运行时中注册了依赖项。有关 DependencyService 的更多信息,请查看这些新的 Microsoft docs pages

【讨论】:

  • 是的,它奏效了。但现在我收到一个错误,“拒绝访问路径“/storage/emulated/0/Download/future_of_js_2018_progress.pdf”。你知道我应该在这里做什么吗?
  • 是的。 Read_External_Storage 和 Write_External_Storage 被选中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
  • 2014-05-15
  • 2016-07-25
相关资源
最近更新 更多