最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to read data from blob file? - Stack Overflow

programmeradmin0浏览0评论

My web application (JavaScript+HTML) is loaded into WebView and runs as a native UWP app. This app creates an image, gets image data as a blob and returns blob path (blob:ms-appx-web://5c9b0d94-65fd-4fa7-aded-837e3428129f/97ed6fc2-8ac9-445b-a3e1-1ff7b1d42d59) to ScriptNotify event of the host application WebView.

I need to read data from this blob file. What is the correct way to read data from the blob file?

My web application (JavaScript+HTML) is loaded into WebView and runs as a native UWP app. This app creates an image, gets image data as a blob and returns blob path (blob:ms-appx-web://5c9b0d94-65fd-4fa7-aded-837e3428129f/97ed6fc2-8ac9-445b-a3e1-1ff7b1d42d59) to ScriptNotify event of the host application WebView.

I need to read data from this blob file. What is the correct way to read data from the blob file?

Share Improve this question asked Apr 22, 2018 at 13:54 KibernetikKibernetik 3,03830 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The blob object is created by Javascript API, and the blob object URL is created by method createObjectURL also in Javascript. This object URL that format with blob:ms-appx-web://... is recognized by HTML but may not be recognized by UWP APIs.

To read the blob data, you could create Data URLs for the image blob object instead. Data URLs prefixed with the data: scheme and data encoded into base64 format. Then in UWP the base64 data could be converted to image source.

Details for how to convert image into base64 string using javascript please reference this thread.The following is just for example:

.html

<head>
    <script type='text/javascript'> 
        function toDataURL(url, callback) {
            var xhr = new XMLHttpRequest();
            xhr.onload = function () {
                var reader = new FileReader();
                reader.onloadend = function () {
                    callback(reader.result);
                }
                reader.readAsDataURL(xhr.response);
            };
            xhr.open('GET', url);
            xhr.responseType = 'blob';
            xhr.send();
        }

        toDataURL('https://....jpeg', function (dataUrl) { 
            window.external.notify(dataUrl);
            document.querySelector("#image").src = dataUrl;
        })
    </script>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <div id="myDiv"> 
        <image id="image" />
    </div>
</body> 

xaml.cs

private void Webview_ScriptNotify(object sender, NotifyEventArgs e)
{
    var result = e.Value;
    resultimage = result.Substring(result.IndexOf(",")+1);
}

private async void btngetblob_Click(object sender, RoutedEventArgs e)
{ 
    byte[] imageBytes = Convert.FromBase64String(resultimage);
    StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, KnownFolderId.PicturesLibrary);
    StorageFile file = await storageFolder.CreateFileAsync("new.jpg", CreationCollisionOption.ReplaceExisting);
    using (IRandomAccessStream randomstream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        DataWriter writer = new DataWriter(randomstream);
        writer.WriteBytes(imageBytes);
        await writer.StoreAsync();
        await writer.FlushAsync(); 
    }
}
发布评论

评论列表(0)

  1. 暂无评论