给你一段上传代码:
Private Function Up() as Boolean
'遍历File表单元素
Dim files As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files '状态信息
Dim strMsg As New System.Text.StringBuilder("上传的文件分别是:<hr color=red>")
Dim iFile As System.Int32
Try
For iFile = 0 To files.Count - 1
'检查文件扩展名字
Dim postedFile As System.Web.HttpPostedFile = files(iFile)
Dim fileName, fileExtension As System.String
fileName = System.IO.Path.GetFileName(postedFile.FileName)
If Not (fileName = String.Empty) Then
fileExtension = System.IO.Path.GetExtension(fileName)
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>")
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>")
strMsg.Append("上传文件的文件名:" + fileName + "<br>")
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>")
'可根据扩展名字的不同保存到不同的文件夹
TxtFileURL.Text = "../DownLoadFile/" + fileName
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../DownLoadFile/") + fileName)
End If
Next
strStatus.Text = strMsg.ToString()
Return True
End Function
Private Function Up() as Boolean
'遍历File表单元素
Dim files As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files '状态信息
Dim strMsg As New System.Text.StringBuilder("上传的文件分别是:<hr color=red>")
Dim iFile As System.Int32
Try
For iFile = 0 To files.Count - 1
'检查文件扩展名字
Dim postedFile As System.Web.HttpPostedFile = files(iFile)
Dim fileName, fileExtension As System.String
fileName = System.IO.Path.GetFileName(postedFile.FileName)
If Not (fileName = String.Empty) Then
fileExtension = System.IO.Path.GetExtension(fileName)
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>")
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>")
strMsg.Append("上传文件的文件名:" + fileName + "<br>")
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>")
'可根据扩展名字的不同保存到不同的文件夹
TxtFileURL.Text = "../DownLoadFile/" + fileName
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../DownLoadFile/") + fileName)
End If
Next
strStatus.Text = strMsg.ToString()
Return True
End Function
解决方案 »
- 字符串拆分显示
- <pages enableEventValidation= "true "/>
- 在.net程序中如何执行.bat文件啊?
- asp.net2003的网页打不开
- 无法输出nvarchar(max)的值?
- 菜鸟提问:ASP.NET 运行时错误: 该应用程序的配置文件必须包含“trust”节 什么意思?
- ASP.net 文字大小的限制
- 存储过程返回值 怎么老是0?晕死,
- ASP.NET的安全性问题
- 如何在AJAX中实现多输出
- 谁能帮我改改这句代码 int ProductID =(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex];
- 如何限制只显示标题的前几个字符.
得到stream。然后就可以保存了。
客户的下载工具回监测到
{
public class MyUpload
{
private System.Web.HttpPostedFile postedFile=null;
private string savePath="";
private string extension="";
private int fileLength=0;
//显示该组件使用的参数信息
public string Help
{
get{
string helpstring;
helpstring="<font size=3>MyUpload myUpload=new MyUpload(); //构造函数";
helpstring+="myUpload.PostedFile=file1.PostedFile;//设置要上传的文件";
helpstring+="myUpload.SavePath=\"e:\\\";//设置要上传到服务器的路径,默认c:\\";
helpstring+="myUpload.FileLength=100; //设置上传文件的最大长度,单位k,默认1k";
helpstring+="myUpload.Extension=\"doc\";设置上传文件的扩展名,默认txt";
helpstring+="label1.Text=myUpload.Upload();//开始上传,并显示上传结果</font>";
helpstring+="<font size=3 color=red>Design By WengMingJun 2001-12-12 All Right Reserved!</font>";
return helpstring;
}
}public System.Web.HttpPostedFile PostedFile
{
get
{
return postedFile;
}
set
{
postedFile=value;
}
}public string SavePath
{
get
{
if(savePath!="") return savePath;
return "c:\\";
}
set
{
savePath=value;
}
}public int FileLength
{
get
{
if(fileLength!=0) return fileLength;
return 1024;
}
set
{
fileLength=value*1024;
}
}public string Extension
{
get
{
if(extension!="") return extension;
return "txt";
}
set
{
extension=value;
}
}public string PathToName(string path)
{
int pos=path.LastIndexOf("\\");
return path.Substring(pos+1);
}public string Upload()
{
if(PostedFile!=null)
{
try{
string fileName=PathToName(PostedFile.FileName);
if(!fileName.EndsWith(Extension)) return "You must select "+Extension+" file!";
if(PostedFile.ContentLength>FileLength) return "File too big!";
PostedFile.SaveAs(SavePath+fileName);
return "Upload File Successfully!";
}
catch(System.Exception exc)
{return exc.Message;}
}
return "Please select a file to upload!";
}
}
}用csc /target:Library Wmj.cs 编译成dll供以后多次调用
调用举例
<%@page language="C#" runat="server"%>
<%@import namespace="Wmj"%>
<script language="C#" runat="server">
void Upload(object sender,EventArgs e)
{
MyUpload myUpload=new MyUpload();
// label1.Text=myUpload.Help;
myUpload.PostedFile=file1.PostedFile;
myUpload.SavePath="e:\\";
myUpload.FileLength=100;
label1.Text=myUpload.Upload();
}
</script>
<form enctype="multipart/form-data" runat="server">
<input type="file" id="file1" runat="server"/>
<asp:Button id="button1" Text="Upload" OnClick="Upload" runat="server"/>
<asp:Label id="label1" runat="server"/>
</form>
结论:asp.net的组件支持功能很强 我们如果充分利用可以很容易的编写出非常方便的组件可以大大的方便我们的工作