<td class="style1">
<asp:TextBox runat="server" ID ="dataurl" Width="50%" Visible="false"></asp:TextBox>
<asp:FileUpload ID="FileUpload1" runat="server" Width="65%"/>
<asp:Button ID="Button3" runat="server" Text="上传" OnClick="UpLoad" Height="25" Width="25%"/>
<asp:Label ID ="lab2" runat="server" Text="上传成功!" ForeColor="Red" Visible="false"></asp:Label>
</td>后台代码: /// <summary>
/// 保存文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
protected void UpLoad(object sender, EventArgs e)
{
foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
{
string Path = Server.MapPath(@"../../Uploads") //如果路径不存在,则创建
if (System.IO.Directory.Exists(Path) == false)
{
System.IO.Directory.CreateDirectory(Path)
}//file.GetName()取得文件名
string filename = file.GetName().ToString() //取得文件名(包括路径)里最后一个"."的索引
int index = filename.LastIndexOf(".")
//取得文件扩展名
string extendName = filename.Substring(index) //取得原文件名不包含后缀名
string fileNameFirst = filename.Substring(0, index) //用当前时间为文件重名名,确保文件名不重复
string datename = DateTime.Now.ToString("yyyyMMddHHmmss") string newFileName = fileNameFirst + datename + extendName
//组合路径
Path = Path + "/" + newFileName //保存
file.SaveAs(Path, true) this.dataurl.Visible = true
this.lab2.Visible = true
this.FileUpload1.Visible = false
this.Button3.Visible = false
this.dataurl.ReadOnly=true
this.dataurl.Text = newFileName //Response.Write("f1:" + fileNameFirst)
//Response.Write("f2:" + Path)
}
}
这个要用到“保存文件对话框”控件来实现,它的作用是将文件保存到指定的位置。private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = "C:\\"//初始化目录;
saveFileDialog1.Filter = "bmp文件(*.bmp)|*.bmp|jpg文件(*.jpg)|*.jpg"//允许的文件类型;
saveFileDialog1.FilterIndex = 1
saveFileDialog1.RestoreDirectory = true
saveFileDialog1.ShowHelp = true
saveFileDialog1.Title = "保存图片"
saveFileDialog1.FileName = ""
saveFileDialog1.ShowDialog()
}
如果您只想单纯靠asp.netde 方法也很多的我给你提供些一个比较简单的方法 你可以参考一下
前台自动生成 FILE的控件(需要多少生成多少)
<script>
function add()
{
var str = '<li style="width:200pxmargin-left:129pxdisplay:inline"><INPUT name="upload" runat="server" type="file" size="50" style="border:1px solid #7F9DB9width:260px"/></li>'
document.getElementById('w').insertAdjacentHTML("beforeEnd",str)
}
</script>
<input type="button" style="border:1px solid #7F9DB9" margin-left:42px value="继续上传文件" onclick="add()" />
后台实现上传
public string SaveImages()
{
System.Web.HttpFileCollection files = HttpContext.Current.Request.Files
//System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件分别是:<hr color=red>")
// System.Int32 iFile
try
{
//strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>")
//strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>")
//strMsg.Append("上传文件的文件名:" + fileName + "<br>")
//strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>")
////此处地址也可以自行更改,此例中请在程序目录里新建"upload"目录即可
// string aa=System.Guid.NewGuid().ToString()+fileExtension
string FileName=""
for (int iFile = 0iFile <= files.Count - 1iFile++)
{
System.Web.HttpPostedFile postedFile = files[iFile]
System.String fileName
System.String fileExtension
fileName = System.IO.Path.GetFileName(postedFile.FileName)
if (!((System.IO.Path.GetFileName(postedFile.FileName) == string.Empty)))
{
// bool flag = FileUpTool.File_Up(postedFile, Server.MapPath("../ImageUpload/"), ".gif,.jpg,.ico,.png", ref FileName)
fileExtension = System.IO.Path.GetExtension(fileName)
string aa = fileName
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../Upload/") + aa)
FileName += aa + ","
}
}
//strStatus.Text = strMsg.ToString()
return FileName
}
catch (System.Exception Ex)
{
// strStatus.Text = Ex.Message
return "上传失败"
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)