最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net javascript 文件無刷新上傳實(shí)例代碼

 更新時間:2009年06月13日 19:47:05   作者:  
最近在寫C# .net代碼的時候,遇到一個上傳刷新的問題。

第二種方法:用js動態(tài)創(chuàng)建form和iframe上傳文件,實(shí)現(xiàn)無刷新。優(yōu)點(diǎn)是代碼量小,無需客戶端安裝控件,缺點(diǎn)就是上傳有限制大小,下面看代碼:
需要文件有:1個前臺頁面upload.html、 1個js函數(shù) function upFile、 1個處理文件accept.aspx(accept.aspx.cs)
upload.html
復(fù)制代碼 代碼如下:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標(biāo)題頁</title>
<script src="upload.js" src="upload.js" language="jscript" type="text/jscript"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="width:100%">
<iframe name='hidden_frame' id="hidden_frame" style='width:150px; height:50px; display:none;'> </iframe>
<input type="file" id="hidFilePath" />
<input id="upBtn" type="button" class="clearBtn" style="display:none;" value="上傳圖片" onclick="upFile('hidFilePath');" />
</div>
</form>
</body>
</html>

function upFile
復(fù)制代碼 代碼如下:

function upFile(ob)
{
var file = document.getElementById(ob) ;
var newName = "FileName"; //設(shè)置文件保存的名字

var form=document.createElement('form');
document.body.appendChild(form);
form.encoding="multipart/form-data";
form.method = "post";
form.action= "accept.aspx?nm=" + newName;
form.target= "hidden_frame";
var pos=file.nextSibling; //記住file在舊表單中的的位置
form.appendChild(file);
form.submit();
pos.parentNode.insertBefore(file,pos);
document.body.removeChild(form);
}

accept.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="up.aspx.cs" Inherits="Member_up" %>
accept.aspx.cs
復(fù)制代碼 代碼如下:

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 Member_up : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mz = HttpContext.Current.Request.QueryString["nm"].ToString();
string uperr = "";
HttpFileCollection files = HttpContext.Current.Request.Files;

if (files.Count>0)
{ uperr = upSingleFile(files[0], mz); }
else { uperr = "ok"; }
HttpContext.Current.Session["upInfo"] = uperr;
Response.Write(uperr);
}


//上傳文件
private string upSingleFile(HttpPostedFile file, String theFileName)
{
string infos = "";
bool fileOK = false;

string fileName, fileExtension ;
fileName = System.IO.Path.GetFileName(file.FileName);
if (fileName != "")
{
if (file.ContentLength >= 1024 * 1024 * 2)
{
infos = "上傳文件太大,目前僅支持2M以內(nèi)的圖片上傳!";
}
else
{
fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
String[] allowedExtensions = { ".jpg", ".jpeg", ".gif", ".bmp", ".png", ".icon" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
break;
}
}
if (!fileOK)
{
infos = "不支持上傳此類型文件!目前支持的圖片格式有:jpg|jpeg|gif|bmp|png|icon";
}
else
{
file.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/books/BookPic/") + theFileName);
infos = "ok" + theFileName;
}
}
}
else
{
infos = "沒有讀取到文件!";
}
return infos;
}
}

以上為我寫程序過程中遇到過的問題和探索到的解決方法,寫下來一是自己在溫習(xí)鞏固一遍,二來是為了與大家分享,請大家指正需改進(jìn)之處,以期達(dá)到共同進(jìn)步!

相關(guān)文章

最新評論

镇沅| 临泉县| 延安市| 丽江市| 贺兰县| 永仁县| 如东县| 舞阳县| 襄城县| 都昌县| 罗定市| 察隅县| 内江市| 邢台市| 定边县| 岳西县| 秦皇岛市| 农安县| 偏关县| 崇礼县| 桓仁| 内江市| 沭阳县| 清水河县| 岳池县| 凤山县| 宁阳县| 九龙城区| 县级市| 昆山市| 乐陵市| 牟定县| 临城县| 天津市| 武平县| 绿春县| 滦南县| 黔南| 三江| 东海县| 五峰|