首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#上传有关问题?小弟我如何提交不到服务器(代码在文章中)

2012-01-13 
c#上传问题?我怎么提交不到服务器(代码在文章中)up2.aspx%@Pagelanguage c# Codebehind up2.aspx.cs

c#上传问题?我怎么提交不到服务器(代码在文章中)
up2.aspx

<%@   Page   language= "c# "   Codebehind= "up2.aspx.cs "   AutoEventWireup= "false "   Inherits= "uploadtest.up2 "   %>
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN "   >
<HTML>
<HEAD>
<title> up2 </title>
<meta   name= "GENERATOR "   Content= "Microsoft   Visual   Studio   .NET   7.1 ">
<meta   name= "CODE_LANGUAGE "   Content= "C# ">
<meta   name= "vs_defaultClientScript "   content= "JavaScript ">
<meta   name= "vs_targetSchema "   content= "http://schemas.microsoft.com/intellisense/ie5 ">
</HEAD>
<body   MS_POSITIONING= "GridLayout ">
<form   id= "Form1 "   method= "post "   runat= "server ">
<table>
<tr   bgcolor= "#ffffff ">
<td   height= "24 ">
<input   type= "file "   id= "Up_file "   class= "edline "   runat= "server "   style= "WIDTH:   456px;   HEIGHT:   20px "
size= "60 "   name= "Up_file ">
</td>
</tr>
<TR   bgcolor= "#ffffff ">
<TD   height= "24 "   align= "center ">
<asp:Button   ID= "submit "   Runat= "server "   Text= "文件上传 "   CssClass= "Cmdbut "   Height= "20px "> </asp:Button>
</TD>
</TR>
</table>
</form>
</body>
</HTML>

up2.aspx.cs

using   System;
using   System.Collections;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Web;
using   System.Web.SessionState;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.HtmlControls;
using   System.IO;
namespace   uploadtest
{
///   <summary>
///   up2   的摘要说明。
///   </summary>
public   class   up2   :   System.Web.UI.Page
{
protected   System.Web.UI.WebControls.Button   submit;
protected   System.Web.UI.HtmlControls.HtmlInputFile   Up_file;

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
//   在此处放置用户代码以初始化页面
}
private   void   submit_Click(object   sender,   System.EventArgs   e)
{
HttpPostedFile   postedFile   =   this.Up_file.PostedFile;   //得到要上传文件
string   fileName,   fileExtension;
fileName   =   System.IO.Path.GetFileName(postedFile.FileName);   //文件名称

if   (fileName   !=   " ")
{
fileExtension   =   System.IO.Path.GetExtension(fileName);   //上传文件的扩展名
string   new_filename   =   fileExtension;   //给文件重新命名
//postedFile.FileName:   客户端文件地址
//postedFile.ContentType.ToString():   上传的文件类型
//保存文件到文件夹,地址是当前页面的同一级目录下的files文件夹中

postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath( "aaa/ ")   +   new_filename);  


//.....可以把文件的相应信息保存到数据库中去。
}





}
#region   Web   窗体设计器生成的代码
override   protected   void   OnInit(EventArgs   e)
{
//
//   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   InitializeComponent()
{        
this.Load   +=   new   System.EventHandler(this.Page_Load);

}
#endregion
}
}


[解决办法]
<form id= "Form1 " method= "post " runat= "server " enctype= "multipart/form-data ">

加个属性

[解决办法]
楼上正解~
[解决办法]
<form id= "Form1 " method= "post " runat= "server " enctype= "multipart/form-data ">
[解决办法]
出什么错??错误提示呢??贴出来look look
[解决办法]
贴个我从前写的代码,用的FileUpload控件,限定wmv/wma,如果不加限定,去掉contenttype的if就行了。

string str = Server.MapPath( ". ") + "\\source\\ " + Path.GetFileName(myUpLoad.PostedFile.FileName); ;
if (myUpLoad.HasFile == true)
{
string fileContentType = myUpLoad.PostedFile.ContentType;
if (fileContentType == "audio/x-ms-wma " || fileContentType == "video/x-ms-wmv ")
{
if (File.Exists(str) == false)
try
{
myUpLoad.SaveAs(str);
}
catch (Exception es)
{
lblInfo.Text = es.Message;
}
else lblInfo.Text = "File already exists. ";
}
else lblInfo.Text = "It must be a wma or wmv file. ";
}

热点排行