In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "HTML5 how to achieve upload file display progress", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "HTML5 how to upload file display progress" this article.
Here we combine Asp.net MVC as the server, and you can also be other server languages. Let's take a look at the HTML of this segment:
@ using (Html.BeginForm ("Upload", "Home", FormMethod.Post, new {enctype = "multipart/form-data", id= "form1"}))
{
Upload Image:
}
The relevant Javascript is as follows:
Function fileSelected () {
Var file = document.getElementById ('fileToUpload'). Files [0]
If (file) {
Var fileSize = 0
If (file.size > 1024 * 1024)
FileSize = (Math.round (file.size * 100 / (1024 * 1024)) / 100). ToString () + 'MB'
Else
FileSize = (Math.round (file.size * 100 / 1024) / 100). ToString () + 'KB'
Document.getElementById ('fileName') [xss_clean] =' Name:'+ file.name
Document.getElementById ('fileSize') [xss_clean] =' Size:'+ fileSize
Document.getElementById ('fileType') [xss_clean] =' Type:'+ file.type
}
}
Function uploadFile () {
Var fd = new FormData ()
Fd.append ("fileToUpload", document.getElementById ('fileToUpload') .files [0])
Var xhr = new XMLHttpRequest ()
Xhr.upload.addEventListener ("progress", uploadProgress, false)
Xhr.addEventListener ("load", uploadComplete, false)
Xhr.addEventListener ("error", uploadFailed, false)
Xhr.addEventListener ("abort", uploadCanceled, false)
Xhr.open ("POST", "Home/Upload")
Xhr.send (fd)
}
Function uploadProgress (evt) {
If (evt.lengthComputable) {
Var percentComplete = Math.round (evt.loaded * 100 / evt.total)
Document.getElementById ('progressNumber') [xss_clean] = percentComplete.toString () +'%'
}
Else {
Document.getElementById ('progressNumber') [xss_clean] =' unable to compute'
}
}
Function uploadComplete (evt) {
/ * This event is raised when the server send back a response * /
Alert (evt.target.responseText)
}
Function uploadFailed (evt) {
Alert ("There was an error attempting to upload the file.")
}
Function uploadCanceled (evt) {
Alert ("The upload has been canceled by the user or the browser dropped the connection.")
}
Above is the native Javascript, the function of fileSelected is executed in the onchange event, and the function of uploadFile is executed by clicking button. Here, ajax is used to upload files using XMLHttpRequest. Note that the code works on Firefox 14. IE 9 does not currently support file api. You can participate here. The server-side code is simple:
Public class HomeController: Controller
{
Public ActionResult Index ()
{
Return View ()
}
/ / /
/ Uploads the specified files.
/ / /
/ The files.
/ ActionResult
[HttpPost]
Public ActionResult Upload (HttpPostedFileBase [] fileToUpload)
{
Foreach (HttpPostedFileBase file in fileToUpload)
{
String path = System.IO.Path.Combine (Server.MapPath ("~ / App_Data"), System.IO.Path.GetFileName (file.FileName))
File.SaveAs (path)
}
ViewBag.Message = "File (s) uploaded successfully"
Return RedirectToAction ("Index")
}
}
The above is all the contents of the article "how to upload files to show progress in HTML5". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.