當前位置:首頁 » 文件管理 » html上傳文件原理

html上傳文件原理

發布時間: 2022-11-12 17:09:56

㈠ 求高手指教,網頁上傳文件是如何實現的

其實有時候答案應該自己去尋找,你既然知道做法,直接抓個包不就知道流程了嗎,

  1. 選擇文件之後,瀏覽器只是得到了文件的路徑,就是你看到的文本框里的值.

  2. 點擊上傳後,會給伺服器發送類似以下格式的封包,這是原始封包,伺服器收到之後,會進行解析才會到達動態腳本,比如php,

以下是我上傳了一張桌面上的placeholder.gif圖片文件,所發送給伺服器的封包.裡麵包括了文件的一些信息和內容.你看下.他們用HTTP頭信息里的分隔符分開.

POST /up.php HTTP/1.1

Accept:*/*

Referer: http://www.xxx.com/

Accept-Language: zh-cn

Content-Type: multipart/form-data; boundary=---------------------------7df2ef0c00e4

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

Host: www.xxx.com

Content-Length: 10810

Connection: Keep-Alive

Cache-Control: no-cache

Cookie: lang=zh-cn; _ga=GA1.2.447688366.1439125779; _gat=1; Hm_lvt_=1439125779; Hm_lpvt_=1439125779

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="UPLOAD_IDENTIFIER"

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="langkey"

1

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="setcookie"

1

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="tempvar"

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="upfile"; filename="C:Documents and SettingsAdministrator......placeholder.gif"

Content-Type: image/gif

(文件二進制數據)

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="fpath"

C:Documents and SettingsAdministrator......placeholder.gif

-----------------------------7df2ef0c00e4--

㈡ html中怎麼上傳文件代碼

在HTML標准中,XMLHttpRequest對象被重新定義,被稱為「XMLHttpRequest Level 2」,其中包含了以下5個新特性:

1、支持上傳、下載位元組流,比如文件、blob以及表單數據。

2、增加了上傳、下載中的進度事件。

3、跨域請求的支持。

4、允許發送匿名請求(即不發送HTTP的Referer部分)。

5、允許設置請求的超時。

    在這篇教程中,我們主要關注第一和第二項特性,尤其是第二項——它能夠提供我們想要的上傳進度。和之前的方案不同,這個方案並不要求伺服器作出特殊的設置,因此大家邊看教程就可以邊動手試試了。

    上面圖示的就是我們能夠實現的內容:

    1、顯示上傳的文件信息,比如文件名、類型、尺寸。

    2、一個能夠顯示真實進度的進度條。

    3、上傳的速度。

    4、剩餘時間的估算。

    5、已上傳的數據量。

    6、上傳結束後伺服器返回的響應。

    另外,憑借XMLHttpRequest,我們的上傳過程整個都是非同步的,因此用戶在上傳文件的時候,依然可以操作網頁當中的其它元素,並不需要專門等待上傳的完成。而在上傳結束後,我們能夠獲取伺服器發回的響應,因此整個上傳過程都顯得相當順理成章。

㈢ 文件上傳的基本原理,用文字詳細說明一下

我藉助別人的資料,感覺挺詳細,仔細研究一下吧
中文件上傳與下載1。上傳文件流程

先通過用戶提交文件,保存文件到伺服器端,然後在寫入資料庫中,每次到下載頁面時從資料庫中讀出文件,生成文件在伺服器目錄中,以下。。。

文件上傳後保存文件到伺服器中jsp,

upFile.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" %>
< import="java.util.List;"%>

<html>
<head>
<title>上傳文件</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<base target="_self">
<link href="Css/style.css" rel="stylesheet">
<script type="text/javascript">
function check(){
var file = document.getElementById("file1").value;
if(file == ""){
alert('請選擇要上傳的文件');
return false;
}else{
window.returnValue=file;
return true;
}
}
</script>
</head>
<body>
<%
String files=request.getParameter("files");
%>
<form name="form2" enctype="multipart/form-data" method="post" action="../MissionManage/commonfile.jsp?files=<%=files %>" onsubmit="return check();">
<center>
<table align="center" width="350" height="150" border="0" cellpadding="0" cellspacing="0"><!-- background="images/upFile_bg.gif -->
<tr>
<td valign="top"><table width="100%" height="145" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="49" colspan="2"></td>
</tr>
<tr>
<td width="9%" height="53"></td>
<td width="91%"><b>請選擇上傳的文件:</b><br>
<input id="file1" name="file1" type="file" size="35" onkeydown="return false;">
<br>
註:文件大小請控制在10M以內。</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="Submit" type="submit" class="btn_grey" value="確認">

<input name="Submit2" type="button" class="btn_grey" onClick="window.close()" value="關閉"></td>
</tr>
</table></td>
</tr>
</table>
</center>
</form>
</body>
</html>

commonfile.jsp

<%@ page language="java" import="java.io.*" pageEncoding="UTF-8"%>
< import="java.util.*,org.apache.commons.fileupload.FileItem" %>
< import="java.text.SimpleDateFormat,java.util.Date" %>
< import="com.PoliceSystem.tools.FileOperate" %>
<jsp:useBean id="factory" scope="page" class="org.apache.commons.fileupload.disk.DiskFileItemFactory" />
<jsp:useBean id="upload" scope="page" class="org.apache.commons.fileupload.servlet.ServletFileUpload" />
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>上傳文件</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<base target="_self">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" language="javascript">

</script>
</head>

<body>
<%
request.setCharacterEncoding("UTF-8");
String path1 = (String)request.getRealPath("/upload1");

String files=request.getParameter("files");
String[] fileList=files.split(";");

File file = new File(path1);

if(!file.exists()){
file.mkdirs();
}
factory.setRepository(file);
factory.setSizeThreshold(1024*1024);

upload.setFileItemFactory(factory);

try{
List<FileItem> list= upload.parseRequest(request);

for(FileItem item:list){
if(item.isFormField()){
//String value=item.getString("UTF-8");
//session.setAttribute("fileName", value);
}else{
String value=item.getName();
int start=value.lastIndexOf("\\");
String fileName=value.substring(start+1);
if(fileName.length() == 0 || fileName == ""){
out.println("<script>alert('上傳失敗');window.close();</script>");
}else{
if(item.getSize()>10000000){
out.println("<script>alert('對不起,您上傳的文件超過10M,無法完成上傳!');window.close();</script>");
}else{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date =new Date();
String[] f = item.getName().split("\\\\");
//System.out.println(f[f.length-1]);
String oldFileName = f[f.length-1];
for(int i=0;i<fileList.length;i++){
if(fileList[i].indexOf(oldFileName)!=-1){
out.println("<script>alert('對不起,您上傳的文件與現有上傳的文件重名,請更換文件名重新上傳!');window.returnValue='';window.close();</script>");
}
}
String fType = FileOperate.getFileType(item.getName());//文件類型
fileName = sdf.format(date)+"."+fType;//新文件名
//System.out.println(item.getName()+"---"+fileName);
item.write(new java.io.File(path1,fileName));
//////--1--/////
String mailFileNames = new String();
String old = (String)session.getAttribute("fuJianFileNames");
if(old!=null){
mailFileNames = old;
}
mailFileNames+=oldFileName+"|"+fileName+";";
//System.out.println("mailFileNames="+mailFileNames);
session.removeAttribute("fuJianFileNames");
session.setAttribute("fuJianFileNames", mailFileNames);
//////--2--/////
//String pathName = path1+";
//System.out.println("pathName="+pathName);
out.println("<script>window.close();</script>");
}
}
}
}
}catch (Exception e) {
e.printStackTrace();
out.println("<script>window.close();</script>");
}
%>
</body>
</html>

插入文件數據到資料庫中

public boolean insert_annex(String[] str){
boolean b=true;
String sql ="";
con = db.getConn();
try{
sql="insert into annex (querykey,sfilename,committime,Filetype,filepath,pno,Annex) values (?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
for(int i=0;i<str.length-1;i++){
ps.setString(i+1, str[i]);
}
File file = new File(str[6]);//附件

InputStream iso = new FileInputStream(file);
ps.setBinaryStream(7, iso, iso.available());
ps.execute();
iso.close();
System.out.println("刪除臨時文件:" + file.delete());// 刪除tmp文件
}catch(Exception e){
b=false;
e.printStackTrace();
}finally{
this.close();
}
return b;
}

讀出文件數據,並保存

public boolean addTempFile_annex(String querykey,HttpServletRequest request){
boolean b=true;
con = db.getConn();
InputStream in = null;
OutputStream out= null;
String path1 = (String)request.getRealPath("/upload1");//文件下載臨時目錄
try{
String sql="select filePath,annex from annex where querykey='"+querykey+"'";
//String sql_1="select annex from annex where querykey='"+querykey+"'";

ps = con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
File f = new File(path1+rs.getString("filepath"));
in=rs.getBinaryStream("annex");
out = new FileOutputStream(f);
int len = 10 * 100 * 100; //定義字元數組長度
byte[] P_Buf=new byte[len];
int j;
while((j=in.read(P_Buf))!=-1){
out.write(P_Buf, 0, j);
}
}
in.close();
out.flush(); //強制清出緩沖區
out.close();
}catch(Exception e){
b=false;
e.printStackTrace();
}finally{
this.close();
}
return b;
}

給定傳遞過來的參數(文件名,文件存儲在伺服器的文件名,文件在伺服器的路徑,文件類型),下載文件Action

package com.PoliceSystem.action.mail;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class downLoadFile extends ActionSupport{
private static final long serialVersionUID = -2207648627734251737L;

public String execute() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();

String fileminitype = request.getParameter("fileType");
String filename1 = new String(request.getParameter("fileName1").getBytes("ISO8859-1"),"UTF-8");
String filename2 = new String(request.getParameter("fileName2").getBytes("ISO8859-1"),"UTF-8");
String filepath = request.getRealPath("/upload1");
File f = new File(filepath+");
Long filelength = f.length();
int cacheTime = 10;

response.setContentType(fileminitype);
response.setHeader("Location",filename1);
response.setHeader("Cache-Control", "max-age=" + cacheTime);

response.setContentType("application/octet-stream");
byte[] b = filename1.getBytes("GBK");
filename1 = new String(b,"8859_1");
response.setHeader("Content-Disposition", "attachment;filename=" + filename1);
response.setContentLength(filelength.intValue());
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(f);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}

outputStream.flush();
outputStream.close();
inputStream.close();

return null ;
}
}

㈣ html網頁如何做到上傳文件

HTML只能做表單。要上傳到伺服器,還必須要伺服器端執行的語句文件。如:ASP、PHP等。

㈤ http文件上傳的原理是什麼請問向qq郵箱這類可以上傳文件的網頁,他們上傳文件的原理是不是,當用

有個誤區:
在上傳插件中,用戶點擊提交(確認)後,文件才開始上傳,這時候,會上傳到伺服器的某個目錄(不是臨時目錄,一般是伺服器按照管理用戶文件的規則所建立的文件夾),伺服器會返回一個指針,這個指針指向了你上傳的文件。當你發送郵件後,郵件中也會包含這個指針。不管你把這個文件放到哪個文件夾,實際上都只是存放的這個指針而已。如非管理需要,這個文件應該會永久保存,任何轉發,轉儲的行為,其實也只是操作的這個指針而不是文件本身。
即使你刪除文件,也只是在你的文件夾中刪除這個指針。所以,很多「極速秒傳」實際上是先判斷你要上傳的文件的MD5值是否已經在伺服器中存在,如果存在,就把已存在的文件的指針放上去,是不是很快?

㈥ HTML靜態網頁中怎麼實現上傳文件功能,需要判斷後綴名!

必須使用編程語言,例如php、asp、asp.net或者jsp之類的。

㈦ html中input上傳圖片什麼原理啊php後台怎麼處理如果用ajax的話是傳些什麼

用input上傳圖片是把圖片作為文件傳輸的,在php後台中使用 $_FILES來接收。

注意:前端的form表單除了action ,method 屬性外,還要添加一個'enctype'屬性,否則文件傳輸不成功。

<form enctype="multipart/form-data">

<input type="file" >

</form>

$_FILES接收信息 有幾個屬性:

name , 上傳的文件名稱

size ,上傳的文件大小

tmp_name ,臨時路徑

type ,文件類型

error錯誤提示

error取值說明:

( 0:沒問題。1/2:大小超過限制[1->超出php.ini限制。2->超出文件域max_file限制]。3:只上傳部分附件(不好測試)。4:沒有上傳附件)

有上傳信息時:$_FILES接收到的附件信息:


保存附件:把上傳的文件由臨時路徑保存到真實的圖片存儲的位置。

move_uploaded_file(臨時路徑名附件,真實路徑名附件)

㈧ http文件上傳的原理

HTTP 協議定義伺服器端和客戶端之間文件傳輸的溝通方式。目前HTTP協議的版本是Http1.1。RFC 2616描述了HTTP協議的具體信息。

這個協議已經成為瀏覽器和Web站點之間的標准。

當我上網的時候底層是如何進行交互的?

訪問者點擊一個超鏈接的時候,將會給瀏覽器提交一個URL地址。通過這個URL地址,瀏覽器便知道去鏈接那個網站並去取得具體的頁面文件(也可能是一張圖片,一個pdf文件)。

HTTP工作的基礎就是,連接一個伺服器並開始傳輸文件到瀏覽器。

HTTP傳輸的基本過程

在http傳輸的過程中,被稱為客戶端的請求者向伺服器請求一個文件。

最基本的過程是:
1 客戶端連接一個主機;
2 伺服器接收連接,
3 客戶端請求一個文件,
4 伺服器發送一個應答.

㈨ html有關多個文件上傳

//改好了沒問題..加文件試試
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script type="text/javascript">

function imgChange(next) {
if(next != null)
document.getElementById(next).style.display = "";
}

function validate() {
var phos = 2;
for(i = 0; i < 2; i++) {
if($("del" + i).checked == true)
phos--;
}
for(i = 0; i < 4; i++) {
if($("photoFile" + i).value != "")
phos++;
}
if(phos > 4) {
alert("圖片太多,您最多總共可以保存4張圖片!");
return false;
}
return true;
}
</script>

<form action="/addTrade.do" method="post" enctype="multipart/form-data" onSubmit="return validate()">
<!-- 上傳照片-->
<div>

<table width="100%" border="0" cellspacing="0" cellpadding="0"
summary="upload pictures">
<tr id="tr_photoFile0">
<td width="119" align="right" class="title">
上傳照片:
</td>

<td width="499">
<input type="file" name="photoFile0" id="photoFile0"
size="40" onChange='return imgChange("tr_photoFile1")' />
</td>
</tr>
<tr id="tr_photoFile1" style="display: none;">
<td>

</td>
<td>

<input type="file" name="photoFile1" id="photoFile1"
onChange='return imgChange("tr_photoFile2")' size="40" />
</td>
</tr>
<tr id="tr_photoFile2" style="display: none;">
<td>

</td>
<td>
<input type="file" name="photoFile2" id="photoFile2"
onChange='return imgChange("tr_photoFile3")' size="40" />

</td>
</tr>
<tr id="tr_photoFile3" style="display: none;">
<td>

</td>
<td>
<input type="file" name="photoFile3" id="photoFile3"
onChange='return imgChange(null)' size="40" />
</td>

</tr>
</table>
</div>

<!--/ 上傳照片-->
<input name="submit" type="submit" value="提交"/>
</form>

㈩ html里input標簽里的file控制項的實現原理

不需要建立
臨時文件
,file
控制項
本身只是獲取一下你要上傳文件的路徑,至於上傳就屬於
後台
操作了。

熱點內容
xp系統開機密碼怎麼設置 發布:2024-05-02 06:49:48 瀏覽:759
柱加密區公式 發布:2024-05-02 06:40:19 瀏覽:4
java位元組轉換 發布:2024-05-02 06:40:11 瀏覽:687
用c語言做的程序 發布:2024-05-02 06:26:10 瀏覽:325
解壓用流量 發布:2024-05-02 06:21:46 瀏覽:316
雲購源碼系統 發布:2024-05-02 06:12:52 瀏覽:105
電腦如何進行安卓升級 發布:2024-05-02 06:10:08 瀏覽:37
元龍第5集免費看完整版緩存 發布:2024-05-02 06:03:47 瀏覽:668
腳本宣傳片 發布:2024-05-02 05:56:26 瀏覽:570
有線投屏安卓手機如何設置 發布:2024-05-02 05:43:26 瀏覽:896