最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Angular & JavaScript | Open Blob URL View PDF and Images in New Tab

    正文概述 掘金(Alen__Andry)   2021-07-18   574

    August 31, 2020 Jolly.Exe

    In this JavaScript quick tutorial, we’ll learn how to select a file using a File input control to convert it into a Base64 URL, also add a View button to preview the selected file by opening in the new Chrome tab by creating a BLOB url.

    Article compatible with Angular version starting 4+ up to latest version including 6,7,8,9,10,11 and 12.

    In the HTML forms, where we have upload document or image functionality, we usually convert the selected file into a Base64 encoded url to upload it as a string to the remote server, where the base64 string is converted into a file and saved on remote disk space.

    We have already discussed how to convert the selected file into a Base64 string URL here, you can check the complete tutorial in Angular.

    While filling and selecting the files into form, we may need to provide a preview link using which we can display the selected file to the user in a new browser tab. This can be easily done on the Client-end by converting the File into a Blob object URL.

    Summary of content

    1) What is Blob?

    2) Adding a Form

    3) Adding JavaScript

    3.1) # Create a Blob URL

    4) Final onchange function

    5) Angular Application

    6) Dealing with Unsafe URL’s in Angular

    7) Make Unsafe URLs Trusted

    8) Conclusion

    9) Related Popular Tutorials

    We’ll create a sample for a simple JavaScript application and Angular as well. In Angular application, we face unsafe link scenarios which can be resolved by URL sanitization.

     

    What is Blob?

    Binary Large Object(Blob) is an Object used to store or holding data in a browser. Blobs can be used to read then save data on disk.

    A Blob object has properties to represent the size and MIME type of stored file. This can be used as a normal file.

     

    Adding a Form

    Let’s create a simple HTML form with input control of type file. This file control is having a change event handler to convert the selected file into a base64 string. The onchange event will take care to convert the file into base64 anf Blog.

    <form onsubmit="myFunction(event)">
    
        <label>Select a File</label>
        <input type="file" name="myfile" onchange="fileChangeEvent(event)">
        <div id="selected-file">
        </div>
        <button>Upload</button>
    
    </form>
    Copy
    

     

    Adding JavaScript

    Now add the fileChangeEvent() function which will do the main task for fetching the Base64 URL and Blob based URL.

    The FileReader() function is used to read files and then load in the browser’s memory. It returns a callback method onloadinside of which we’ll get selected file information.

    function fileChangeEvent(fileInput) {
    
      if (fileInput.target.files && fileInput.target.files[0]) {
        const reader = new FileReader();
        reader.onload = (e) => {
          
            ....
            ....
    
        };
        reader.readAsDataURL(fileInput.target.files[0]);
      }
    }
    Copy
    

    Inside the reader.onload, we’ll get  Base64 encoded string URL

    // Base64 String
    console.log(e.target.result);
    Copy
    

     

    Ad by Valueimpression

    # Create a Blob URL

    To create a Blob we call new Blob()then call window.URL.createObjectURL() to convert it into a URL.

    // Create a Blog object for selected file & define MIME type
    var blob = new Blob(fileInput.target.files, { type: fileInput.target.files[0].type });
    
    // Create Blog URL 
     var url = window.URL.createObjectURL(blob);
    Copy
    

     

    Final onchange function

    After the file is selected by a user, we’ll append the selected Angular & JavaScript | Open Blob URL View PDF and Images in New Tab inside the


    起源地下载网 » Angular & JavaScript | Open Blob URL View PDF and Images in New Tab

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元