62,072
社区成员
发帖
与我相关
我的任务
分享
<table>
<tr>
<td>
<asp:Panel runat="server" ID="ImageUpPanel" Height="30px">
信息图片 <asp:FileUpload ID="imageFile" runat="server" Width="400" /></asp:Panel>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnOk" runat="server" Text="确 定" Width="80px" OnClick="btnOk_Click"
Height="30px" />
</td>
</tr>
</table>
protected void btnOk_Click(object sender, EventArgs e)
{
string imgId = Guid.NewGuid().ToString();
string extString = string.Empty;
string imagUrl = string.Empty;
if (!string.IsNullOrEmpty(this.imageFile.FileName.Trim()))
{
int posInt = this.imageFile.FileName.LastIndexOf('.');
if (posInt > -1)
{
extString = postFileName.Substring(posInt + 1);//扩展名
}
imagUrl = @"~/Images/InfoImages/" + imgId + "." + extString;
this.imageFile.SaveAs(Server.MapPath(imagUrl));
}
}
<body>
<form id="form1" runat="server">
<div>
<input id="File1" type="file" name="sz" runat="server" style="width:0px; height:0px; border:0;"/>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" Width=0 /></div>
</form>
</body>
protected void Button1_Click(object sender, EventArgs e)
{
string ss = File1.Value;//下断点可以看到已经获取到客户端路径
files[0].SaveAs("C:\\2.txt");//保存到服务端路径C:\\下
}
<form action="a.aspx" method="post" enctype="multipart/form-data">
<input type="file" />
<input type="submit" value="upload" />
</form>
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = files["Resource"];
if (postedFile.ContentLength > 3072000) FM.Common.Windows.ShowPre("文件大小不能大于300K!");
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
string SaveFilePath = "";
if (fileName != "")
{
SaveFilePath="/UploadFile/"+System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar";
//SaveFilePath=System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar";
fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
if (fileExtension != ".rar") FM.Common.Windows.ShowPre("文件格式不正确,你只能上传WinRar压缩包格式文件!");
//postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("UploadFile/") + SaveFilePath);
postedFile.SaveAs(Server.MapPath(SaveFilePath));
}