10/10/2018, 00:18
Cách viết 1 giao diện cho phép người dùng thực hiện việc UPLOAD file
Cách viết 1 giao diện cho phép người dùng thực hiện việc UPLOAD file lên trang web của mình. Bạn nào có code về phần này chỉ mình với (bằng java hay javascript thì càng tốt). Cám ơn trước nha
Bài liên quan
http://vn2.php.net/manual/en/features.file-upload.php
<?php
if(isset($_POST***91;'btnSubmit'***93;)) {
$ok = 1;
$file_name = $_FILES***91;txtFile***93;***91;name***93;;
$file_size = $_FILES***91;txtFile***93;***91;size***93;;
$file_tmp = $_FILES***91;txtFile***93;***91;tmp_name***93;;
// Kiểm tra xem có file upload chưa
if(!is_uploaded_file($file_tmp)){
echo "Vui lòng chọn file để upload";
$ok = 0;
}
// Kiểm tra dung lượng file = bytes
if($file_size>2000000) {
echo "Vui lòng up file dung lượng dưới 2MB";
$ok = 0;
}
$limit_ext = array(".doc", ".jpg", ".mp3");
// Lấy phần mở rộng của file
$ext = strrchr($file_name, '.');
$ext = strtolower($ext);
// Kiểm tra loại file
if(!in_array($ext, $limit_ext)) {
echo "Bạn chỉ được upload các file .doc, .jpg, .mp3";
$ok = 0;
}
if($ok == 1) {
// Upload vào thư mục images
@move_uploaded_file($file_tmp, "images/".$file_name) or die('Có lỗi xảy ra');
}
}
?>
<html>
<head>
<title>Upload file</title>
</head>
<body>
<form method="POST" action="">
<table>
<tr>
<td><input type="file" name="txtFile" /></td>
<td><input type="submit" name="btnSubmit" value="Upload" /></td>
</tr>
</table>
</form>
</body>
</html>
Tất nhiên Java làm được và làm rất tốt
FileUPload.ASPX
-----------------------------------
<body>
<form id="form1" runat="server">
<div>
<div>
<table style="width: 324px">
<tr>
<td style="width: 298px">
Please select file to upload.</td>
</tr>
<tr>
<td style="width: 298px">
<asp:FileUpload ID="FileUpload1" runat="server" Width="311px" /></td>
</tr>
<tr>
<td style="width: 298px">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" /></td>
</tr>
<tr>
<td style="width: 298px">
<asp:Label ID="Label1" runat="server" Text="Label" Width="263px"></asp:Label></td>
</tr>
</table>
</div>
</div>
</form>
</body>
FileUploadPage.aspx.cs
------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class FileUploadPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String savePath = @"C:\Downloads\";
if (FileUpload1.HasFile)
{
String fileName = FileUpload1.FileName;
savePath += fileName;
HttpPostedFile file = FileUpload1.PostedFile;
if(file.ContentLength<=100000)
{
FileUpload1.SaveAs(savePath);
Label1.Text = "Your file was saved as " + fileName;
}
else
{
Label1.Text = "Sorry, your file was big size ";
}
}
else
{
Label1.Text = "You did not specify a file to upload.";
}
}
}