日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

用aspx开发html5页面,ASP.NET使aspx页面能接受HTML,asp的页面传送的文件-.NET教程,Asp.Net开发...

發布時間:2024/9/15 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用aspx开发html5页面,ASP.NET使aspx页面能接受HTML,asp的页面传送的文件-.NET教程,Asp.Net开发... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

aspx接受aspx頁面的文件很簡單,用htmlinputfile,就可以了,但是如果接受html頁面post的文件

就不怎么好辦了,我仿照asp的方法做法如下,自己測試通過,拿出來給大家共享,可以限制

文件內容,類型,大小,自定義存儲位置,在congfig.xml內

html頁的內容:(來自fckeditor)

fckeditor – uploaders tests

function sendfile()

{

var suploaderurl = cmbuploaderurl.value ;

if ( suploaderurl.length == 0 )

suploaderurl = txtcustomurl.value ;

if ( suploaderurl.length == 0 )

{

alert( please provide your custom url or select a default one ) ;

return ;

}

eurl.innerhtml = suploaderurl ;

txturl.value = ;

frmupload.action = suploaderurl ;

frmupload.submit() ;

}

function onuploadcompleted( errornumber, fileurl, filename, custommsg )

{

switch ( errornumber )

{

case 0 : // no errors

txturl.value = fileurl ;

alert( file uploaded with no errors ) ;

break ;

case 1 : // custom error

alert( custommsg ) ;

break ;

case 10 : // custom warning

txturl.value = fileurl ;

alert( custommsg ) ;

break ;

case 201 :

txturl.value = fileurl ;

alert( a file with the same name is already available. the uploaded file has been renamed to ” + filename + ” ) ;

break ;

case 202 :

alert( invalid file ) ;

break ;

case 203 :

alert( “security error. you probably dont have enough permissions to upload. please check your server.” ) ;

break ;

default :

alert( error on file upload. error number: + errornumber ) ;

break ;

}

}

select the “file uploader” to use:

asp

php

aspx

??????

custom uploader url:

upload a new file:

??????

uploaded file url:

post url: ?

upload.aspx的內容:

下面是后臺代碼:

using system;

using system.data;

using system.configuration;

using system.collections;

using system.io;

using system.text;

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;

using system.xml;

using system.collections.specialized;

public partial class upload : system.web.ui.page

{

public void sendresults(int errornumber, string fileurl, string filename, string custommsg)

{

stringbuilder text = new stringbuilder();

text.append(“

text.append(“window.parent.onuploadcompleted(” + errornumber + “,\”” + fileurl.replace(“\””, “\\\””) + “\”,\”” + filename.replace(“\””, “\\\””) + “\”,\”” + custommsg.replace(“\””, “\\\””) + “\”) ;\n”);

text.append(” ”);

response.write(text.tostring());

response.end();

}

public void getconfig(string type, out string[] allowedext, out string[] denyedext,out string savepath,out long maxsize)

{

xmldocument doc = new xmldocument();

doc.load(server.mappath(@”.\config.xml”));

xmlelement root=doc.documentelement;

xmlnodelist imagenodelist=root.getelementsbytagname(type);

allowedext = imagenodelist[0].firstchild.innertext.trim().split(|);

denyedext = imagenodelist[0].lastchild.innertext.trim().split(|);

savepath = root.getelementsbytagname(“userpath”).item(0).innertext.trim();

try

{

maxsize = convert.toint64(root.getelementsbytagname(“maxsize”).item(0).innertext.trim());

}

catch { maxsize = 10*1024; }

}

protected void page_load(object sender, eventargs e)

{

string[] allowedext = new string[] { }, denyedext = new string[] { };

string savepath = string.empty;

long maxsize = 10000;

string type = request.querystring[“type”];

if(type!=null&&type!=string.empty)

type=type.tolower();

else

type=”file”;

if (type == “image”)

{

getconfig(“image”, out allowedext, out denyedext, out savepath,out maxsize);

}

if (type == “file”)

{

getconfig(“file”, out allowedext, out denyedext, out savepath, out maxsize);

}

if (type == “flash”)

{

getconfig(“flash”, out allowedext, out denyedext, out savepath, out maxsize);

}

if (savepath == string.empty||savepath==””)

savepath = “~/userfiles/”;

if(!savepath.endswith(“/”))savepath+=”/”;

/*********************************************************************************

byte[] bytes1 = system.text.encoding.default.getbytes(“這是字符串\n\n\n\n”);

byte[] bytes2 = new byte[] { 1, 33, 23, 3, 0, 56, 55, 235, 5 };//二進制數

byte[] bytes = new byte[bytes1.length + bytes2.length];

//合并二進制流

memorystream ms = new memorystream(bytes);

ms.write(bytes1, 0, bytes1.length);

ms.write(bytes2, 0, bytes2.length);

int count = 0, pos = 0;

//開始找四個\n

for (int i = 0; i < bytes.length; i++)

{

if (bytes[i] == (int)\n)

{

count++;

if (count == 4)

{

pos -= 4;

break;

}

}

}

if (count == 4)

{

//這里,bytes字節數組里從0 到 pos 的位置就是你要的字符串

//從pos + 5 開始到最后,就是你要的二進制

}

**********************************************************************************/

byte[] filedata, formdata;

formdata = request.binaryread(request.contentlength);

string head = string.empty;

encoding encoding = encoding.utf8;

long pos = 0;

for (long i = 0; i < formdata.longlength; i++)

{

if (formdata[i] == (byte)\r && formdata[i + 1] == (byte)\n && formdata[i + 2] == (byte)\r && formdata[i + 3] == (byte)\n)

{

pos = i;

break;

}

}

if (pos == 0) { response.end(); return; }

head = encoding.getstring(formdata, 0, (int)pos);

filedata = new byte[formdata.longlength – pos – 3];

array.copy(formdata, pos + 4, filedata, 0, formdata.longlength – pos – 4);

/************************************************************************************************

//傳來的表單形式是:

//”—————————–7d5fa3820f84\r\ncontent-disposition: form-data; name=\”newfile\”; filename=\”f:\\documents\\4(10995).jpg\”\r\ncontent-type: image/pjpeg\r\n\r\n

//后面是文件數據

************************************************************************************************/

head = head.tolower();

head = head.remove(0, head.indexof(“\r\n”) + 2);

head = head.replace(“\””, “”);

string postfilename = string.empty;

string filename;//no path

string filetype, fileext;

postfilename = head.substring(0, head.indexof(“\r\n”));//content-disposition: form-data; name=\”newfile\”; filename=\”f:\\documents\\4(10995).jpg\”

filetype = head.remove(0, postfilename.length + 3);//returns:content-type: image/pjpeg

postfilename = postfilename.substring(postfilename.indexof(“filename=”) + “filename=”.length);//c:\path\name

filename = path.getfilename(postfilename);

fileext = filename.substring(filename.lastindexof(“.”) + 1);

if (filedata.longlength > maxsize) {

sendresults(2, resolveurl(savepath + filename), filename, “too large”);

return;

}

bool isallow=false;

foreach(string ext in denyedext){

if (ext == fileext) {

isallow = false;

sendresults(202, resolveurl(savepath + filename), filename, “forrbiden”);

return;

}

}

foreach (string ext in allowedext) {

if (ext == fileext) { isallow = true; break; }

}

if ( isallow)

{

string tmppath = server.mappath(savepath);

if (!directory.exists(tmppath)) {

try

{

directory.createdirectory(tmppath);

}

catch { sendresults(200, resolveurl(savepath + filename), filename, “沒有寫入權限”); }

}

//response.binarywrite(filedata);

filestream savefilestream= new filestream(tmppath+filename, filemode.openorcreate, fileaccess.readwrite);

for (long i = 0; i < filedata.longlength; i++)

{

savefilestream.writebyte(filedata[i]);

}

savefilestream.close();

sendresults(0, resolveurl(savepath + filename), filename, “no errors”);

}

}

}

config.xml

true

500000

zip|rar

php|php3|php5|phtml|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|dll|reg|cgi

jpg|gif|jpeg|png|bmp

swf|fla

總結

以上是生活随笔為你收集整理的用aspx开发html5页面,ASP.NET使aspx页面能接受HTML,asp的页面传送的文件-.NET教程,Asp.Net开发...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。