简体   繁体   中英

UWP c# - Can't use file after being filepicked

I'm trying to upload a file on amazon s3, after filepicked it.

To start, I do this :

       var picker = new Windows.Storage.Pickers.FileOpenPicker();
       picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
       picker.SuggestedStartLocation =
            Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
       picker.FileTypeFilter.Add("*");

       Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();

so here, file is picked and ready to be use.

After that, I try to upload it, like that :

        if (file != null)
        {
            upload(file.Path);
        }
       ...

      public async Task<string> upload(string path)
        {
           TransferUtility utility = new TransferUtility(...);
           await utility.UploadAsync(path,key, bucket); // HERE IS THE PROBLEM

        }

and so , this is the error I catched :

Exception thrown: 'System.AggregateException' in mscorlib.ni.dll System.AggregateException: One or more errors occurred. ---> System.UnauthorizedAccessException: Acces denied.

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at PCLStorage.WinRTFileSystem.d__0.MoveNext()
--- End of inner exception stack trace --- at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task 1.get_Result() at Amazon.S3.Transfer.TransferUtility.validate(TransferUtilityUploadRequest request) at Amazon.S3.Transfer.TransferUtility.GetUploadCommand(TransferUtilityUploadRequest request, SemaphoreSlim asyncThrottler) at Amazon.S3.Transfer.TransferUtility.UploadAsync(String filePath, String bucketName, String key, CancellationToken cancellationToken) at Tilkee.Upload.d__0.MoveNext() ---> (Inner Exception #0) System.UnauthorizedAccessException: Acces denied.

I tried to put it in another Task async, but same result.

Any ideas?

We cannot access files using its path unless the file is in the accessible location like app installation folder, known folders like music lib since UWP apps are isolated and run in sandbox. FileOpenPicker helps in the app to let user choose to broke the app container and access to the files, yes, we can then access this file for example using stream, we can also get the path of this file, but the path here cannot be used to access this file again when you want to upload this file. For more information, you can refer to Skip the path: stick to the StorageFile .

My suggestion is that after the file is picked, access the file stream and try to upload this file stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM