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开发...相关推荐

  1. 【转载】把aspx文件编译成DLL文件-.NET教程,Asp.Net开发

    前言 asp.net不是asp的简单升级,而是微软.net计划中的一个重要组成部分,它依托.net的多语言与强大的类库支持,引进了服务端html控件与web控件,自动处理控件的客户端与服务端的 交互, ...

  2. ASP.NET使aspx页面能接受HTML,asp的页面传送的文件

    aspx接受aspx页面的文件很简单,用HtmlInputFile,就可以了,但是如果接受Html页面post的文件 就不怎么好办了,我仿照asp的方法做法如下,自己测试通过,拿出来给大家共享,可以限 ...

  3. ASP.NET中aspx页面runat=server的本质(Essensial of runat=”server” in ASP.NET)

    今天同事问我一个"神奇"的问题,另一个同事"神奇"地找出了问题但无法解释,归咎于一种"习惯"或者"下次注意".现在我把问 ...

  4. MUI框架开发HTML5手机APP(二)--页面跳转传值底部选项卡切换

    原文链接:   一.MUI加载子页面 1加载子页面详解 在mobile app开发过程中,经常遇到卡头卡尾的页面,也就是说头部和尾部保持不动,而只有中间区域可以滚动,常见的就是新闻列表与详情页等情况: ...

  5. aspx后台调用前台jquery_Jquery Ajax调用aspx页面方法

    在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过aspx.cs中的静态方法+WebMethod进 ...

  6. 《移动网页设计与开发 HTML5+CSS3+JavaScript》—— 1.4  真正的HTML5

    本节书摘来异步社区<移动网页设计与开发 HTML5+CSS3+JavaScript>一书中的第1章,第1.4节,作者:[英]Peter Gasston,更多章节内容可以访问云栖社区&quo ...

  7. html5开发桌面程序调用dll,使用Visual Studio开发Html5应用

    Visual Studio 一直以来是开发微软旗下应用的利器,只要是开发微软相关的应用无论是Windows程序,WPF,Asp.Net,WinRT Surface,WindowsPhone 等微软旗下 ...

  8. JAVA制作网页的软件有哪些,html5开发工具(开发html5网页的软件有哪些)

    html5开发工具 1.SublimeText SublimeText是一个跨渠道的代码编bai辑器,一起支持duWindows.Linux.MacOSX等操作系统,也是HTML和散文zhi先进的文本 ...

  9. 开发HTML5的强大9个开发工具

    HTML5被看做是Web开发者创建流行Web应用的利器,增加了对视频和Canvas 2D的支持.HTML5的诞生还让人们重新审视浏览器专用多媒体插件的未来,如Adobe的Flash和微软的Silver ...

最新文章

  1. EventBus-再也不用什么Handler了
  2. C# 整数与字符串拼接之间的装箱操作
  3. 跨链(5)“蚂蚁区块链”之跨链数据连接服务
  4. chrome postman插件_一款 Postman 的开源替代品: Postwoman
  5. linux 设置环境变量以及查看环境变量
  6. doctype声明、浏览器的标准、怪异等模式
  7. Spring注解浅入浅出——不吹牛逼不装逼
  8. 24岁女孩与30岁男人的精彩对白
  9. Javascript:forEach、map、filter、reduce、reduceRight、find、findIndex、keys、values、entries、every、some的使用
  10. 凸优化第九章无约束优化 9.2 下降方法
  11. ramoops具体失败原因来解释驱动寄存器
  12. 禁止QQ自动转义表情字符
  13. 怎么使用openssl来生成一个自签名的x509证书?
  14. ABAQUS橡胶大变形分析不收敛解决办法_51CAE_新浪博客
  15. 网页游戏老手村《梦幻西游网页版》项目开发经验分享
  16. php 下载的文件损坏,PHP readfile()导致文件下载损坏
  17. DiskGenius备份/还原分区=备份Windows系统
  18. C语言/Python经典算法冒泡排序
  19. linux分屏方式显示2015,Linux下的分屏显示
  20. 博客-hexo-matery主题改造笔记

热门文章

  1. JS的编码:escape,encodeURI,encodeURIComponent,解码:unescape,decodeURI,decodeURIComp
  2. 使用轮转算法求时间片_彩票调度算法,让进程们拼手气? --当操作系统遇上随机算法...
  3. 微信小程序的点击复制功能
  4. 微信支付退款回调处理 php
  5. java properties配置文件_java properties配置文件操作
  6. avws扫描出来的漏洞怎么利用_漏洞扫描利用
  7. linux的无密码登录,linux 无密码登录
  8. excel表中怎么插入visio_用Excel编制精确甘特图,准确控制任务进展,提高项目管理水平...
  9. mysql sort aborted_mysql排序中断(Sort aborted)-mysql临时文件无法写入
  10. 【MySQL】日期函数:日期、时间增减