SlideShare uma empresa Scribd logo
1 de 17
Jason
INTRODUCTION
 The File API could be used to create a
thumbnail preview of images as they're
being sent to the server, or allow an app
to save a file reference while the user is
offline.
Browser Support
 File API are supported in all major
browsers. except Internet Explorer.
 Resources:http://caniuse.com/fileapi
File API
 Blob - Allows for slicing a file into byte ranges.
 File
 FileReader
File API - Blob
var b = new Blob() ;
var b =new Blob(['AAA', 'BBB'],{ 'type': 'text/plain'}) ;
b.type
b.size.
b.slice(start, length)
File API - File
 var file = $(“input[type=file]")[0].files[0];
 file.name
 file.type
 file.size.
 file.slice(start, length)
File API - FileReader
 FileReader.readAsBinaryString(Blob|File)
 The result property will contain the file/blob's
data as a binary string. Every byte is
represented by an integer in the range [0..255].
File API - FileReader
 FileReader.readAsText(Blob|File, opt_encoding)
 The result property will contain the file/blob's
data as a text string. By default the string is
decoded as 'UTF-8'. Use the optional encoding
parameter can specify a different format.
File API - FileReader
 FileReader.readAsDataURL(Blob|File)
 The result property will contain the file/blob's
data encoded as a data URL
Data URI scheme
 http URI scheme
 <img src="http://www.hkpug.net/files/images/check.png"/>
 data URI scheme
 <img
src="data:image/png;base64,iVBORw0KGgoAAAANS
UhEUgAAAAQAAAADCAIAAAA
7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvMAhEQM
YgcACEHG8ELxtbPAAAAAElFTkSuQmCC" />
Data URI scheme
 data:image/png;base64,iVBORw0KGgoAAAANSUh
EUgAAAAQAAAADCAIAAAA
7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvMAhEQ
MYgcACEHG8ELxtbPAAAAAElFTkSuQmCC
 data - 取得 data 的協定名稱
 image/png – data 類型名稱
 base64 – data 的編碼方法
 iVBOR.... - 編碼後的 data
 : , ; - data URI scheme 指定的分隔符號
Data URI scheme
 Data URI scheme 也可以在 CSS 中使用,例
如:
 body { background-image:
url("data:image/png;base64,iVBORw0KGgoAA
AANSUhEUgAAAAQAAAADCAIAAAA
7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvM
AhEQMYgcACEHG8ELxtbPAAAAAElFTkSuQ
mCC");}
File API - FileReader
 FileReader.onloadstart // 讀取操作開始時觸發
 FileReader.onprogress // 讀取操作過程中觸發
 FileReader.onabort // 讀取操作被中斷時觸發
 FileReader.onerror // 讀取操作失敗時觸發
 FileReader.onload // 讀取操作成功時觸發
 FileReader.onloadend // 讀取操作完成時觸發
(不論成功還是失敗)
File API – FileReader States
 FileReader.EMPTY (Value = 0):
No data has been loaded yet.
 FileReader.LOADING (Value = 1):
Data is currently being loaded
 FileReader.DONE (Vlaue = 2):
The entire read request has been
completed.
File API - Example
var file = $("#file")[0].files[0];
var reader = new FileReader();
reader.readAsText (file);
reader.onload = function(){
$("#content").val(reader.result);
};
File API - Blob
var file = new Blob(
["這是建立 Blob 的內容!!"],
{ "type": "text/plain" });
var fileReader = new FileReader();
fileReader.onload = function (event) {
console.log('新建 Blob 檔案內容:'+this.result);
} ;
fileReader.readAsText(file);
Reference
 http://www.w3.org/TR/file-upload/#dfn-
Blob
 http://www.w3.org/TR/FileAPI/#dfn-file

Mais conteúdo relacionado

Destaque

T sql語法之 cte 20140214
T sql語法之 cte 20140214T sql語法之 cte 20140214
T sql語法之 cte 20140214LearningTech
 
Query string httpvaluecollection
Query string httpvaluecollectionQuery string httpvaluecollection
Query string httpvaluecollectionLearningTech
 
Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)LearningTech
 
Implement custom iprincipal in mvc
Implement custom iprincipal in mvcImplement custom iprincipal in mvc
Implement custom iprincipal in mvcLearningTech
 
20120518 advanced jsrendertemplatingfeatures
20120518 advanced jsrendertemplatingfeatures20120518 advanced jsrendertemplatingfeatures
20120518 advanced jsrendertemplatingfeaturesLearningTech
 
20131108 boot strap-basiccss By Randy
20131108 boot strap-basiccss By Randy20131108 boot strap-basiccss By Randy
20131108 boot strap-basiccss By RandyLearningTech
 

Destaque (8)

T sql語法之 cte 20140214
T sql語法之 cte 20140214T sql語法之 cte 20140214
T sql語法之 cte 20140214
 
Query string httpvaluecollection
Query string httpvaluecollectionQuery string httpvaluecollection
Query string httpvaluecollection
 
Js types
Js typesJs types
Js types
 
Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)Auto fac mvc以及進階應用(一)
Auto fac mvc以及進階應用(一)
 
Implement custom iprincipal in mvc
Implement custom iprincipal in mvcImplement custom iprincipal in mvc
Implement custom iprincipal in mvc
 
20120518 advanced jsrendertemplatingfeatures
20120518 advanced jsrendertemplatingfeatures20120518 advanced jsrendertemplatingfeatures
20120518 advanced jsrendertemplatingfeatures
 
20131108 boot strap-basiccss By Randy
20131108 boot strap-basiccss By Randy20131108 boot strap-basiccss By Randy
20131108 boot strap-basiccss By Randy
 
Html basic
Html basicHtml basic
Html basic
 

Semelhante a File api

Configuring Greenstone's OAI server
Configuring Greenstone's OAI serverConfiguring Greenstone's OAI server
Configuring Greenstone's OAI serverDiego Spano
 
Going on an HTTP Diet: Front-End Web Performance
Going on an HTTP Diet: Front-End Web PerformanceGoing on an HTTP Diet: Front-End Web Performance
Going on an HTTP Diet: Front-End Web PerformanceAdam Norwood
 
Django - Python MVC Framework
Django - Python MVC FrameworkDjango - Python MVC Framework
Django - Python MVC FrameworkBala Kumar
 
HTTP/2 What's inside and Why
HTTP/2 What's inside and WhyHTTP/2 What's inside and Why
HTTP/2 What's inside and WhyAdrian Cole
 
Azure Blob Storage API for Scala and Spark
Azure Blob Storage API for Scala and SparkAzure Blob Storage API for Scala and Spark
Azure Blob Storage API for Scala and SparkBraja Krishna Das
 
Web development - technologies and tools
Web development - technologies and toolsWeb development - technologies and tools
Web development - technologies and toolsYoann Gotthilf
 
Spring Boot with Microsoft Azure Integration.pdf
Spring Boot with Microsoft Azure Integration.pdfSpring Boot with Microsoft Azure Integration.pdf
Spring Boot with Microsoft Azure Integration.pdfInexture Solutions
 
The new static resources framework
The new static resources frameworkThe new static resources framework
The new static resources frameworkmarcplmer
 
cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123Parag Gajbhiye
 
SP Rest API Documentation
SP Rest API DocumentationSP Rest API Documentation
SP Rest API DocumentationIT Industry
 
Web Technologies (2/12): Web Programming – HTTP. Cookies. Web Sessions
Web Technologies (2/12): Web Programming – HTTP. Cookies. Web SessionsWeb Technologies (2/12): Web Programming – HTTP. Cookies. Web Sessions
Web Technologies (2/12): Web Programming – HTTP. Cookies. Web SessionsSabin Buraga
 
Posting Images using Android
Posting Images using AndroidPosting Images using Android
Posting Images using AndroidAli Muzaffar
 
BioCASE web services for germplasm data sets, at FAO, Rome (2006)
BioCASE web services for germplasm data sets, at FAO, Rome (2006)BioCASE web services for germplasm data sets, at FAO, Rome (2006)
BioCASE web services for germplasm data sets, at FAO, Rome (2006)Dag Endresen
 
Seguranca em APP Rails
Seguranca em APP RailsSeguranca em APP Rails
Seguranca em APP RailsDaniel Lopes
 
Video Archiving and Playback in the Wayback Machine
Video Archiving and Playback in the Wayback MachineVideo Archiving and Playback in the Wayback Machine
Video Archiving and Playback in the Wayback MachineSawood Alam
 
File uploading through paperclip in rails 3.x
File uploading through paperclip in rails 3.xFile uploading through paperclip in rails 3.x
File uploading through paperclip in rails 3.xAndolasoft Inc
 
Web Standards Support in WebKit
Web Standards Support in WebKitWeb Standards Support in WebKit
Web Standards Support in WebKitJoone Hur
 

Semelhante a File api (20)

Apache Kite
Apache KiteApache Kite
Apache Kite
 
Configuring Greenstone's OAI server
Configuring Greenstone's OAI serverConfiguring Greenstone's OAI server
Configuring Greenstone's OAI server
 
Html5 Basic Structure
Html5 Basic StructureHtml5 Basic Structure
Html5 Basic Structure
 
Going on an HTTP Diet: Front-End Web Performance
Going on an HTTP Diet: Front-End Web PerformanceGoing on an HTTP Diet: Front-End Web Performance
Going on an HTTP Diet: Front-End Web Performance
 
Django - Python MVC Framework
Django - Python MVC FrameworkDjango - Python MVC Framework
Django - Python MVC Framework
 
HTTP/2 What's inside and Why
HTTP/2 What's inside and WhyHTTP/2 What's inside and Why
HTTP/2 What's inside and Why
 
Azure Blob Storage API for Scala and Spark
Azure Blob Storage API for Scala and SparkAzure Blob Storage API for Scala and Spark
Azure Blob Storage API for Scala and Spark
 
Web development - technologies and tools
Web development - technologies and toolsWeb development - technologies and tools
Web development - technologies and tools
 
Spring Boot with Microsoft Azure Integration.pdf
Spring Boot with Microsoft Azure Integration.pdfSpring Boot with Microsoft Azure Integration.pdf
Spring Boot with Microsoft Azure Integration.pdf
 
The new static resources framework
The new static resources frameworkThe new static resources framework
The new static resources framework
 
cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123
 
SP Rest API Documentation
SP Rest API DocumentationSP Rest API Documentation
SP Rest API Documentation
 
Web Technologies (2/12): Web Programming – HTTP. Cookies. Web Sessions
Web Technologies (2/12): Web Programming – HTTP. Cookies. Web SessionsWeb Technologies (2/12): Web Programming – HTTP. Cookies. Web Sessions
Web Technologies (2/12): Web Programming – HTTP. Cookies. Web Sessions
 
Posting Images using Android
Posting Images using AndroidPosting Images using Android
Posting Images using Android
 
BioCASE web services for germplasm data sets, at FAO, Rome (2006)
BioCASE web services for germplasm data sets, at FAO, Rome (2006)BioCASE web services for germplasm data sets, at FAO, Rome (2006)
BioCASE web services for germplasm data sets, at FAO, Rome (2006)
 
Seguranca em APP Rails
Seguranca em APP RailsSeguranca em APP Rails
Seguranca em APP Rails
 
Video Archiving and Playback in the Wayback Machine
Video Archiving and Playback in the Wayback MachineVideo Archiving and Playback in the Wayback Machine
Video Archiving and Playback in the Wayback Machine
 
File uploading through paperclip in rails 3.x
File uploading through paperclip in rails 3.xFile uploading through paperclip in rails 3.x
File uploading through paperclip in rails 3.x
 
Php frameworks
Php frameworksPhp frameworks
Php frameworks
 
Web Standards Support in WebKit
Web Standards Support in WebKitWeb Standards Support in WebKit
Web Standards Support in WebKit
 

Mais de LearningTech

Mais de LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

Último

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 

Último (20)

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

File api

  • 2. INTRODUCTION  The File API could be used to create a thumbnail preview of images as they're being sent to the server, or allow an app to save a file reference while the user is offline.
  • 3. Browser Support  File API are supported in all major browsers. except Internet Explorer.  Resources:http://caniuse.com/fileapi
  • 4. File API  Blob - Allows for slicing a file into byte ranges.  File  FileReader
  • 5. File API - Blob var b = new Blob() ; var b =new Blob(['AAA', 'BBB'],{ 'type': 'text/plain'}) ; b.type b.size. b.slice(start, length)
  • 6. File API - File  var file = $(“input[type=file]")[0].files[0];  file.name  file.type  file.size.  file.slice(start, length)
  • 7. File API - FileReader  FileReader.readAsBinaryString(Blob|File)  The result property will contain the file/blob's data as a binary string. Every byte is represented by an integer in the range [0..255].
  • 8. File API - FileReader  FileReader.readAsText(Blob|File, opt_encoding)  The result property will contain the file/blob's data as a text string. By default the string is decoded as 'UTF-8'. Use the optional encoding parameter can specify a different format.
  • 9. File API - FileReader  FileReader.readAsDataURL(Blob|File)  The result property will contain the file/blob's data encoded as a data URL
  • 10. Data URI scheme  http URI scheme  <img src="http://www.hkpug.net/files/images/check.png"/>  data URI scheme  <img src="data:image/png;base64,iVBORw0KGgoAAAANS UhEUgAAAAQAAAADCAIAAAA 7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvMAhEQM YgcACEHG8ELxtbPAAAAAElFTkSuQmCC" />
  • 11. Data URI scheme  data:image/png;base64,iVBORw0KGgoAAAANSUh EUgAAAAQAAAADCAIAAAA 7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvMAhEQ MYgcACEHG8ELxtbPAAAAAElFTkSuQmCC  data - 取得 data 的協定名稱  image/png – data 類型名稱  base64 – data 的編碼方法  iVBOR.... - 編碼後的 data  : , ; - data URI scheme 指定的分隔符號
  • 12. Data URI scheme  Data URI scheme 也可以在 CSS 中使用,例 如:  body { background-image: url("data:image/png;base64,iVBORw0KGgoAA AANSUhEUgAAAAQAAAADCAIAAAA 7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvM AhEQMYgcACEHG8ELxtbPAAAAAElFTkSuQ mCC");}
  • 13. File API - FileReader  FileReader.onloadstart // 讀取操作開始時觸發  FileReader.onprogress // 讀取操作過程中觸發  FileReader.onabort // 讀取操作被中斷時觸發  FileReader.onerror // 讀取操作失敗時觸發  FileReader.onload // 讀取操作成功時觸發  FileReader.onloadend // 讀取操作完成時觸發 (不論成功還是失敗)
  • 14. File API – FileReader States  FileReader.EMPTY (Value = 0): No data has been loaded yet.  FileReader.LOADING (Value = 1): Data is currently being loaded  FileReader.DONE (Vlaue = 2): The entire read request has been completed.
  • 15. File API - Example var file = $("#file")[0].files[0]; var reader = new FileReader(); reader.readAsText (file); reader.onload = function(){ $("#content").val(reader.result); };
  • 16. File API - Blob var file = new Blob( ["這是建立 Blob 的內容!!"], { "type": "text/plain" }); var fileReader = new FileReader(); fileReader.onload = function (event) { console.log('新建 Blob 檔案內容:'+this.result); } ; fileReader.readAsText(file);