<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void UploadButton_Click(object sender, EventArgs e) { // Specify the path on the server to // save the uploaded file to. String savePath = @"c:\temp\uploads\"; // Before attempting to perform operations // on the file, verify that the FileUpload // control contains a file. if (FileUpload1.HasFile) { // Get the name of the file to upload. String fileName = FileUpload1.FileName; // Append the name of the file to upload to the path. savePath += fileName; // Call the SaveAs method to save the // uploaded file to the specified path. // This example does not perform all // the necessary error checking. // If a file with the same name // already exists in the specified path, // the uploaded file overwrites it. FileUpload1.SaveAs(savePath); // Notify the user of the name of the file // was saved under. UploadStatusLabel.Text = "Your file was saved as " + fileName; } else { // Notify the user that a file was not uploaded. UploadStatusLabel.Text = "You did not specify a file to upload."; } } </script><html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>FileUpload Example</title> </head> <body> <form id="form1" runat="server"> <div> <h4>Select a file to upload:</h4> <asp:FileUpload id="FileUpload1" runat="server"> </asp:FileUpload> <br /><br /> <asp:Button id="UploadButton" Text="Upload file" OnClick="UploadButton_Click" runat="server"> </asp:Button> <hr /> <asp:Label id="UploadStatusLabel" runat="server"> </asp:Label> </div> </form> </body> </html>
上传控件下面就直接有一个属性获得图片名的。 还有个属性是获得全路径。
{
if (fileUpload.HasFile)
{
string savePath = Server.MapPath("~/upload/");
if(!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
savePath = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(savePath);//保存文件
string filename=fileUpload.FileName;
}
}
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Button1_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(FileUpload1.FileName))//判断是否为正确的路径
{
//
//这里判断是否为图片
//
FileUpload1.SaveAs(MapPath("保存路径"));//保存图片
}
}
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>实验</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
{
// Specify the path on the server to
// save the uploaded file to.
String savePath = @"c:\temp\uploads\"; // Before attempting to perform operations
// on the file, verify that the FileUpload
// control contains a file.
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
String fileName = FileUpload1.FileName; // Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath); // Notify the user of the name of the file
// was saved under.
UploadStatusLabel.Text = "Your file was saved as " + fileName;
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
} }
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4> <asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload> <br /><br /> <asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button> <hr /> <asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "dd", "alert('请选择要传的图片!');", true);
return;
} string filePath = FileUpload1.PostedFile.FileName;//获取图片文件的路径
string imgname = filePath.Substring(filePath.LastIndexOf('/') + 1);
//如果不需要重命名的话就直接取imgname这个值就是文件名
string imgtype = filePath.Substring(filePath.LastIndexOf('.') + 1).ToLower();
string savePath = "";//图片要保存的路径
string filename = "";//文件名
if (imgtype == "jpg" || imgtype == "jpeg" || imgtype == "gif" || imgtype == "bmp")
{
filename = DropDownListSort.SelectedValue + "_" + DropDownListBrand.SelectedValue + "_" + DateTime.Now.ToFileTime().ToString() + "." + imgtype;//给图片重命名
savePath = Server.MapPath("~/images/Goods/" + filename);//要保存的物理文件路径
FileUpload1.PostedFile.SaveAs(savePath);//保存图片
}
String Filename=FilePath.SubString(FilePath.LastIndexOf(“\\”)+1;
String ServerPath=Server.MapPath(@”~\...\ftp\”)+Filename;
FileUpload1.PostFile.SaveAs(ServerPath);