首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将图片上传到Imgur C# UWP上

将图片上传到Imgur C# UWP上
EN

Stack Overflow用户
提问于 2022-02-08 10:40:20
回答 2查看 153关注 0票数 1

我一直试图匿名上传一个图像到Imgur上,使用Imgur来工作,但是我一直面临着未经授权的路径访问问题。

我尝试过在Microsoft Docs上搜索其他类似的文章,并在这里搜索堆栈溢出的帖子,但没有找到解决方案。我甚至将我的应用程序"broadFileSystemAccess“作为在我的Package.appxmanifest中重新设置的功能,这是我在阅读Microsoft文档时发现的。

我收到的错误是:

System.UnauthorizedAccessException:‘访问路径'C:\Users\lysyr\Pictures\ROG Logo.png’被拒绝。

错误发生在var filecon = File.ReadAllBytes(imgpath);行。

我的文件选择代码是:

代码语言:javascript
运行
复制
public static string imgpath = "";

    public static string finalimg = "";

    private async void FileNameButton_Click(object sender, RoutedEventArgs e)
    {
        var picker = new Windows.Storage.Pickers.FileOpenPicker();
        picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
        picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
        picker.FileTypeFilter.Add(".jpg");
        picker.FileTypeFilter.Add(".jpeg");
        picker.FileTypeFilter.Add(".png");

        Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
        if (file != null)
        {
            // Application now has read/write access to the picked file
            imgpath = file.Path;
            var filecon = File.ReadAllBytes(imgpath); #Error drops here <---
            finalimg = Convert.ToBase64String(filecon);

            await ImgurUploadAPI();
            Debug.WriteLine("Picked Image: " + file.Name);
            uploadedimage_text.Text = "Picked Image: " + file.Name;
        }
        else
        {
            Debug.WriteLine("Image uploading has been cancelled.");
        }
    }

ImgurUpload任务代码是:

代码语言:javascript
运行
复制
public static string imgurlink = "";

    public async Task ImgurUploadAPI()
    {
        try
        {
            if (imgpath != null)
            {
                // Construct the HttpClient and Uri
                HttpClient httpClient = new HttpClient();
                Uri uri = new Uri("https://api.imgur.com/3/upload");

                httpClient.DefaultRequestHeaders.Add("Authorization", "Client-ID IMGUR-CLIENTIDHERE");
                //Debug.WriteLine("Request Headers: ");

                // Construct the JSON to post
                HttpStringContent content = new HttpStringContent("image=\"{finalimg}\"");
                Debug.WriteLine("Request Upload: " + content);

                // Post the JSON and wait for a response
                HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
                    uri,
                    content);

                // Make sure the post succeeded, and write out the response
                httpResponseMessage.EnsureSuccessStatusCode();
                var httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync();
                imgurlink = httpResponseBody;
                Debug.WriteLine("Request Response: " + httpResponseBody);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }

我觉得这可能与我访问和转换图像准备上传的方式有关。任何提示都会非常感谢,因为我已经在这个问题上坚持了一段时间,这是最后一件事,我需要完成我的项目。干杯!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-08 18:19:51

使用UWP时,对文件系统的访问是有限的。这意味着您不能像在这里所做的那样,在任意路径上简单地读取文件:

代码语言:javascript
运行
复制
var filecon = File.ReadAllBytes(imgpath);

相反,您需要做的是让从StorageFile接收到的FilePicker对象获得一个read。就像这样:

代码语言:javascript
运行
复制
var buffer = await FileIO.ReadBufferAsync(file);
var filecon = buffer.ToArray();
finalimg = Convert.ToBase64String(filecon);

您可以在微软文档。上找到有关UWP文件访问的更多信息。

票数 1
EN

Stack Overflow用户

发布于 2022-02-09 02:47:14

此外,朱利安·西尔登·朗洛回答。对于您的场景还有另一种选择。此行为的原因是无法使用File.ReadAllBytes(String) Method直接访问文件。

我注意到您已经添加了broadFileSystemAccess功能,您需要注意的是,此功能只适用于Windows.Storage API。所以你只能用它

代码语言:javascript
运行
复制
StorageFile file = StorageFile.GetFileFromPathAsync(filepath)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71032472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档