Đối với ứng dụng web và di động cầm tay thì vấn đề tải lại một file là rất đặc trưng như với nhiều nhu cầu không giống nhau từ tín đồ sử dụng: đăng hình sản phầm, logo, tập tin thống kê... Và nhờ một ít sự giúp đỡ từ Javascript thì bên HTML cũng hỗ trợ cách cài đặt 1 tệp lên trang web của bạn. Cùng nhau tò mò ở nội dung bài viết này nhé!

*

1. Mua 1 file

Chúng ta rất có thể dùng tag và chọn định dạng là "file" để hoàn toàn có thể upload tệp tin lên website hay vận dụng của mình.

Bạn đang xem: Blob url là gì

input type="file" id="file-uploader">Với cách thực hiện này cho phép người dùng gồm nút download lên một hoặc nhiều tệp cơ mà theo mang định nó chất nhận được tải lên một tệp duy nhất bởi trình duyệt y tệp nơi bắt đầu của hệ điều hành.

Khi upload lên thành công, API file giúp rất có thể đọc File bằng phương pháp sử dụng mã Java
Script đối chọi giản. Đầu tiên, rước file theo id:

const file
Uploader = document.get
Element
By
Id("file-uploader");Sau đó, thêm event để đọc đối tượng người sử dụng tệp khi quy trình tải lên trả tất. Lấy tin tức file được tải lên từ thuộc tính event.target.files.

file
Uploader.add
Event
Listener("change", (event) => const files = event.target.files; console.log("files", files););Bạn hoàn toàn có thể xem cụ thể ở kiểm tra nhé!

2. Tải nhiều file

Khi họ sử dụng biện pháp trên nhưng hy vọng upload lên những file một lúc thì chỉ cần thêm nằm trong tính này trong tag

input type="file" id="file-uploader" multiple />Như vậy, trình lưu ý file sẽ chất nhận được bạn upload lên một hoặc các tệp một lúc. Cũng tương tự trường hòa hợp trên thì chúng ta có thể thêm event thay đổi để nắm bắt thông tin về các file được sở hữu lên. Đối với tương đối nhiều tệp sở hữu lên thì File
List được xem như là một mảng.

Bạn hoàn toàn có thể xem chi tiết ở kiểm tra nhé!

3. File metadata

Khi nào chúng ta upload một file lên trên trình duyệt, tệp tin đó sẽ sở hữu được thông tin siêu dữ liệu như tên file, kích thước, thời gian cập nhật lần cuối, loại, v.v. Tin tức này hoàn toàn có thể hữu ích đến việc chứng thực thêm và ra quyết định.

// Get the file uploader by idconst file
Uploader = document.get
Element
By
Id("file-uploader");// Listen khổng lồ the change event and read metadatafile
Uploader.add
Event
Listener("change", (event) => // Get the File
List array const files = event.target.files; // Loop through the files & get metadata for (const tệp tin of files) const name = file.name; const type = file.type ? file.type: "NA"; const size = file.size; const last
Modified = file.last
Modified; console.log( file, name, type, size, last
Modified ); );Bạn hoàn toàn có thể xem chi tiết ở kiểm tra nhé!

4. Tệp tin accept property

Chúng ta hoàn toàn có thể sử dụng ở trong tính accept để số lượng giới hạn loại tệp sở hữu lên. Dùng trong các trường họp như khi chúng ta chỉ mong mỏi hiển thị một số loại hình hình ảnh trong hiện tượng khi người dùng tải lên hình ảnh hồ sơ.

input type="file" id="file-uploader" accept=".jpg, .png" multiple>Như trong khúc code trên, trình xem xét file đã chỉ được cho phép các file bao gồm phần không ngừng mở rộng là jpg và png.

Lưu ý, vào trường hợp này, trình lưu ý file auto đặt loại lựa lựa chọn file là cấu hình thiết lập thay bởi tất cả. Tuy nhiên, bạn luôn có thể đổi khác nó trở lại toàn bộ các file, trường hợp cần.

Bạn có thể xem cụ thể ở test nhé!

5. Quản ngại lí nội dụng file

Sau khi upload tệp tin lên thành công xuất sắc thì bạn có nhu cầu hiển thị câu chữ file lên, như trong số trường hợp bắt buộc hiển thị nội dung, hình hình ảnh khi download lên hồ nước sơ cá thể chẳng hạn.Chúng ta rất có thể sử dụng đối tượng File
Reader để chuyển đổi tệp sang trọng chuỗi nhị phân. Sau đó, thêm sự kiện xử lý để cài đặt chuỗi nhị phân lên file thành công.

// Get the instance of the File
Readerconst reader = new File
Reader();file
Uploader.add
Event
Listener("change", (event) => const files = event.target.files; const tệp tin = files<0>; // Get the tệp tin object after upload & read the // data as URL binary string reader.read
As
Data
URL(file); // Once loaded, vị something with the string reader.add
Event
Listener("load", (event) => // Here we are creating an image tag and adding // an image to lớn it. Const img = document.create
Element("img"); image
Grid.append
Child(img); img.src = event.target.result; img.alt = file.name; ););Bạn xem cụ thể ở demo nhé!

6. Validate file size

Như các bạn đã thấy, bạn có thể đọc được kích thước của một file, chúng ta thực sự rất có thể sử dụng nó để xác thực kích cỡ file. Bạn có thể cho phép người tiêu dùng tải lên tệp hình hình ảnh có dung lượng tối đa 1MB.

// Listener for tệp tin upload change eventfile
Uploader.add
Event
Listener("change", (event) => // Read the file kích thước const file = event.target.files<0>; const kích cỡ = file.size; let msg = ""; // kiểm tra if the file kích thước is bigger than 1MB & prepare a message. If (size > 1024 * 1024) msg = `The allowed file form size is 1MB. The tệp tin you are trying khổng lồ upload is of $return
File
Size(size)`; else msg = ` A $return
File
Size(size) tệp tin has been uploaded successfully. `; // Show the message lớn the user feedback.inner
HTML = msg;);Bạn rất có thể xem cụ thể ở demo nhé!

7. Cách xử lý file với object
URLs

Có một phương thức đặc biệt được điện thoại tư vấn là URL.create
Object
URL () để sinh sản một URL tốt nhất từ tệp. Bạn cũng có thể sử dụng nó bằng cách sử dụng cách thức URL.revoke
Object
URL ().

URL.create
Object
URL () và URL.revoke
Object
URL () methods có thể chấp nhận được bạn tạo các chuỗi URL đối chọi giản hoàn toàn có thể được áp dụng để tham chiếu bất kỳ dữ liệu nào có thể được tham chiếu đến bằng phương pháp sử dụng đối tượng người sử dụng file DOM, bao gồm các file toàn cục trên máy tính của bạn dùng.

Xem thêm: Tra Từ Loading Terminal Là Gì, Phân Biệt Thuật Ngữ Port Và Terminal

img.src = URL.create
Object
URL(file);Bạn hoàn toàn có thể xem chi tiết ở chạy thử nhé!

Tổng kết

Ở phía trên là 1 trong những số chia sẻ về việc upload tệp tin lên trang web hay áp dụng của bạn, hy vọng sẽ giúp ích được cho một trong những bạn. Nội dung bài viết được xem thêm tại đây.

I"m using safari, & unsure if this works in other browsers, but when I signed up for a storm path account, I see the following URL

blob:https://api.stormpath.com/1be95204-93d6-4GUID HEREThe blob: prefix is new khổng lồ me & I want to lớn know if it works across all browsers, and what other prefixes might exist.


*

Blob URI/URL was created by Java
Script, refers to lớn data that your browser currently has in memory (only in current page), & does not refer khổng lồ data the exists on the host.

For more information to use, see:https://www.npmjs.com/package/blob-util


*

The start of a URL up to lớn the first colon is called a "URI scheme" (or "URL scheme"). Googling for blob URI scheme reveals the W3C"s tệp tin API efforts.

Most URI schemes don"t have standard provisions for containing a whole other URI, so you wouldn"t think of them as prefixing another whole URI. Besides http: and https:, some common URI schemes include:

ftp:mailto:data:script:smb: (and cifs: )afp:

…and many more. I"m sure I"m leaving out everyone"s favorites. :)


*

I copy và paste the answer from Stack
Overflow because of background info about blob in URL string.

A URL that was created from a Java
Script Blob can not be converted to a "normal" URL.

A blob: URL does not refer khổng lồ data the exists on the server, it refers lớn data that your browser currently has in memory, for the current page. It will not be available on other pages, it will not be available in other browsers, và it will not be available from other computers.

Therefore it does not make sense, in general, to lớn convert a Blob URL to lớn a "normal" URL. If you wanted an ordinary URL, you would have to send the data from the browser to a server và have the hệ thống make it available lượt thích an ordinary file.

It is possible convert a blob: URL into a data: URL, at least in Chrome. You can use an AJAX request to "fetch" the data from the blob: URL (even though it"s really just pulling it out of your browser"s memory, not making an HTTP request).


mô tả
Improve this answer
Follow
edited May 23, 2017 at 12:41
*

Community
Bot
1
answered Aug 4, năm 2016 at 17:10
*

Sybil
Sybil
88022 gold badges99 silver badges2323 bronze badges
1
địa chỉ cửa hàng a comment |
3
MDN is a good resource for understanding things in plain language. I would also suggest https://developer.mozilla.org/en-US/docs/Web/API/URL/create
Object
URL as a good resource. They also show the W3C specification at the bottom of their page, the same one as Spiff provided, but the original specifications can be verbose and difficult to lớn read. I would say Spiff is the most correct for linking you to the W3C docs.

I don"t think that the link to npmjs that Masoud provided is very useful. It directs you lớn a Javascript library on the library host npmjs. The blob-util library is used to lớn wrap around the standard API in the browser to work with blobs and it simplified writing code for these things, but it is not a good reference lớn understand the blob URLs và what they are.


nói qua
Improve this answer
Follow
answered Apr 30, 2019 at 20:40
ADJenks
ADJenks
27733 silver badges44 bronze badges
địa chỉ a comment |

You must log in to lớn answer this question.


Not the answer you're looking for? Browse other questions tagged .
The Overflow Blog
Related
2
default "top sites/home page" in Safari
2
A Safari Bookmark Icon That Cannot Be Deleted
0
Sahi doesn't work with Safari 5.0.1 on Mac
OSX
0
Chrome error when resolving URLs for the first time
5
How does my browser know I've visited a page if the URL is redirecting?
0
Chrome and Safari show the same behavior on OS X
0
Getting "This site can’t be reached" error when accessing a page on weibo.com
0
Image not showing when path is put as URL in Safari, loads fine in Chrome
1
DNS resolution when there is no A-record
Hot Network Questions more hot questions

Question feed
Subscribe to RSS
Question feed to lớn subscribe lớn this RSS feed, copy and paste this URL into your RSS reader.


Super User
Company cookie-settings#toggle" class="s-btn s-btn__link py4 js-gps-track -link" data-gps-track="footer.click( location: 2, link: 38 )" data-consent-popup-loader="footer">Cookie Settings
Stack Exchange Network
Site thiết kế / biểu tượng logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA. Rev2024.4.26.8280