怎么用c#在WEB上实现文件的上传功能?HTTP即可! 也发给我一份吧[email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给我email,最大4G的文件,很快 MSDN中就有代码:ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpgenref/html/cpconhtmlinputfilecontrol.htm bool checkexist(string path) { FileInfo fi=new FileInfo(path); if(fi.Exists) return true; else return false; } void onupload(object sender, System.EventArgs e) { if(this.fileUpload.PostedFile.ContentLength!=0) { string filepath; string filename; string [] temp; filename=fileUpload.PostedFile.FileName; temp=filename.Split( '\\'); filename=temp[(temp.Length)-1]; filepath = "c:\\temp\\" + filename; if(checkexist(filepath)) { this.Label1.Text="duplicate filename exists!"; return; } fileUpload.PostedFile.SaveAs(filepath); Label1.Text="thank u! "+this.tb.Value; return; } Label1.Text="no valid file!"; } <%@page language="C#" debug="true"%><HTML> <HEAD> <TITLE> </TITLE> </HEAD><BODY><script language="C#" runat="Server"> //Event Handler for the upload button void UploadFile(object Sender,EventArgs E) { if (File1.PostedFile !=null) //Checking for valid file { // PostedFile.FileName gives the entire path.Use Substring function to rip of the filename. string StrFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") + 1) ; string StrFileType = File1.PostedFile.ContentType ; int IntFileSize =File1.PostedFile.ContentLength; //Checking for the file length if (IntFileSize <=0) Response.Write(" <font color='Red'>Uploading of file " + StrFileName + " failed </font>"); else { File1.PostedFile.SaveAs(Server.MapPath(".\\" + StrFileName)); Response.Write( "<font color='green'>Your file " + StrFileName + " of type " + StrFileType + " and size " + IntFileSize.ToString() + " was uploaded successfully</font>"); } } }</script><H2 align="center">File uploading in ASP.Net using C# - Demo</H2><!-- Declaration of server side form.Note the enctype attribute of the form has to be set to multipart/form-data --><form id="FrmFileUploadDemo" name="FrmFileUploadDemo" method="post" enctype="multipart/form-data" runat="server"> <TABLE align="center" bgcolor="lightyellow"> <TR> <TD> Select a file to upload <input type="file" id="File1" name="File1" runat="server"> </TD> <TR> <TR> <TD align="center"> <input type=button value="Upload" runat="server" id="CmdUpload" onserverclick="UploadFile" /> </TD> </TR> </TABLE></form></BODY></HTML> http://dotnet.aspx.cc/ShowDetail.aspx?id=58EA3515-36F2-4FD9-AC89-EAF49F59816C 哈哈,给一份,[email protected] html<input type="file" runat="server" id="file1">c#if (file1.PostedFile.ContentLength > 0) file1.PostedFile.SaveAs(filename); //判断是否存在同名文件public static bool IsExists(string _FileName){ bool boolReturn = false; FileInfo fi = new FileInfo(_FileName); if(fi.Exists) { boolReturn = true; } return boolReturn;}using System;using System.IO;using System.Web;using System.Web.UI;using GL.ProjectHelper.CommonWay;namespace ProjectHelper.ControlHelper{ /// <summary> /// Summary description for GInputFile. /// </summary> public sealed class InputFile { #region Property Fields private static HttpPostedFile _PostedFile; public static HttpPostedFile PostedFile { get { return _PostedFile; } set { _PostedFile = value; } } public static string ContentType { get { return PostedFile.ContentType; } } public static string FileName { get { return PostedFile.FileName; } } public static string SaveName { get { return DateTime.Now.ToString("yyyyMMddHHmmss") + GString.GetRandom(6) + Path.GetExtension(FileName); } } #endregion /// <summary> /// 返回存储路径 /// </summary> /// <param name="p">页面对象</param> /// <param name="strFileName">文件名</param> /// <returns>服务器端存储路径</returns> private static string GetSavePath(Page p, string strFolder) { string strPath = p.Server.MapPath("/" + strFolder); if(!Directory.Exists(strPath)) { Directory.CreateDirectory(strPath); } return strPath + "\\" + SaveName; } /// <summary> /// 保存文件 /// </summary> /// <param name="strFolder">文件夹名称</param> /// <returns>保存成功?</returns> public static bool SaveAs(Page p, string strFolder) { bool boolReturn = false; if(PostedFile.ContentLength > 0) { PostedFile.SaveAs(GetSavePath(p, strFolder)); boolReturn = true; } return boolReturn; } }} 怎么实现按下鼠标左键并拖动界面时,界面会跟着移动 求两算法,一算法能生成一串随机数,然后另一算法验证字符是否上一个算法生成的 C#中怎么样改变DataGridView中某些单元格的格式? 我是新手:关于网络连接的问题 listview 如何去掉水平滚动条 点击查看是出现的问题 如何用OLEDB来UPDATE一个已经存在的EXCEL? C#中 API函数 findwindos() 重复执行的问题? Binding的Format事件什么时候触发? C#蓝牙开发问题:由于目标主机坏了,套接字操作失败 怎么写这个程序啊 C#编写游戏程序如何?
{
FileInfo fi=new FileInfo(path);
if(fi.Exists) return true;
else return false;
}
void onupload(object sender, System.EventArgs e)
{
if(this.fileUpload.PostedFile.ContentLength!=0)
{
string filepath;
string filename;
string [] temp;
filename=fileUpload.PostedFile.FileName;
temp=filename.Split( '\\');
filename=temp[(temp.Length)-1];
filepath = "c:\\temp\\" + filename;
if(checkexist(filepath))
{
this.Label1.Text="duplicate filename exists!";
return; }
fileUpload.PostedFile.SaveAs(filepath);
Label1.Text="thank u! "+this.tb.Value;
return;
}
Label1.Text="no valid file!";
}
<HTML>
<HEAD>
<TITLE>
</TITLE>
</HEAD>
<BODY>
<script language="C#" runat="Server">
//Event Handler for the upload button
void UploadFile(object Sender,EventArgs E)
{
if (File1.PostedFile !=null) //Checking for valid file
{
// PostedFile.FileName gives the entire path.Use Substring function to rip of the filename.
string StrFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") + 1) ;
string StrFileType = File1.PostedFile.ContentType ;
int IntFileSize =File1.PostedFile.ContentLength;
//Checking for the file length
if (IntFileSize <=0)
Response.Write(" <font color='Red'>Uploading of file " + StrFileName + " failed </font>");
else
{
File1.PostedFile.SaveAs(Server.MapPath(".\\" + StrFileName));
Response.Write( "<font color='green'>Your file " + StrFileName + " of type " + StrFileType + " and size " + IntFileSize.ToString() + " was uploaded successfully</font>");
}
}
}
</script>
<H2 align="center">File uploading in ASP.Net using C# - Demo</H2>
<!-- Declaration of server side form.Note the enctype attribute of the form has to be set to multipart/form-data -->
<form id="FrmFileUploadDemo" name="FrmFileUploadDemo" method="post" enctype="multipart/form-data" runat="server">
<TABLE align="center" bgcolor="lightyellow">
<TR>
<TD>
Select a file to upload <input type="file" id="File1" name="File1" runat="server">
</TD>
<TR>
<TR>
<TD align="center">
<input type=button value="Upload" runat="server" id="CmdUpload" onserverclick="UploadFile" />
</TD>
</TR>
</TABLE>
</form>
</BODY>
</HTML>
[email protected]
<input type="file" runat="server" id="file1">c#
if (file1.PostedFile.ContentLength > 0)
file1.PostedFile.SaveAs(filename);
public static bool IsExists(string _FileName)
{
bool boolReturn = false;
FileInfo fi = new FileInfo(_FileName);
if(fi.Exists)
{
boolReturn = true;
} return boolReturn;
}
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using GL.ProjectHelper.CommonWay;namespace ProjectHelper.ControlHelper
{
/// <summary>
/// Summary description for GInputFile.
/// </summary>
public sealed class InputFile
{
#region Property Fields private static HttpPostedFile _PostedFile;
public static HttpPostedFile PostedFile
{
get
{
return _PostedFile;
}
set
{
_PostedFile = value;
}
} public static string ContentType
{
get
{
return PostedFile.ContentType;
}
} public static string FileName
{
get
{
return PostedFile.FileName;
}
} public static string SaveName
{
get
{
return DateTime.Now.ToString("yyyyMMddHHmmss") + GString.GetRandom(6) + Path.GetExtension(FileName);
}
} #endregion /// <summary>
/// 返回存储路径
/// </summary>
/// <param name="p">页面对象</param>
/// <param name="strFileName">文件名</param>
/// <returns>服务器端存储路径</returns>
private static string GetSavePath(Page p, string strFolder)
{
string strPath = p.Server.MapPath("/" + strFolder); if(!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
} return strPath + "\\" + SaveName;
} /// <summary>
/// 保存文件
/// </summary>
/// <param name="strFolder">文件夹名称</param>
/// <returns>保存成功?</returns>
public static bool SaveAs(Page p, string strFolder)
{
bool boolReturn = false;
if(PostedFile.ContentLength > 0)
{
PostedFile.SaveAs(GetSavePath(p, strFolder));
boolReturn = true;
} return boolReturn;
}
}
}