FileUpload

获得服务器路径

string path = Server.MapPath("~/bc/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path += FileUpload1.FileName; FileUpload1.SaveAs(path);

 

微软做法

 <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <mce: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 save the file, verify // that the FileUpload control contains a file. if (FileUpload1.HasFile) { // Get the size in bytes of the file to upload. int fileSize = FileUpload1.PostedFile.ContentLength; // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded. if (fileSize < 2100000) { // Append the name of the uploaded file to the path. savePath += Server.HtmlEncode(FileUpload1.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 that the file was uploaded successfully. UploadStatusLabel.Text = "Your file was uploaded successfully."; } else { // Notify the user why their file was not uploaded. UploadStatusLabel.Text = "Your file was not uploaded because " + "it exceeds the 2 MB size limit."; } } else { // Notify the user that a file was not uploaded. UploadStatusLabel.Text = "You did not specify a file to upload."; } } // --></mce:script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>FileUpload Class 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>

 

另一种方法网上找的 js取得客户端路径

 

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <!--获取文件上传地址--> <mce:script type="text/javascript"><!-- function checkFile() { //判断浏览器类型 var isIE = (document.all) ? true : false; var isIE7 = isIE && (navigator.userAgent.indexOf('MSIE 7.0') != -1); var isIE8 = isIE && (navigator.userAgent.indexOf('MSIE 8.0') != -1); var file=document.getElementById("UpLoadBorrow"); var path=file.value; if(isIE7 || isIE8) { file.select(); path=document.selection.createRange().text; document.selection.empty(); } document.getElementById("txtFilePath").value=path; } function checkNull() { var path=document.getElementById("txtFilePath").value; if(path=="" ||path==null) { alert('请选择要上传的文件!'); return false; } return true; } // --></mce:script> </head> <body style="background-color:#0099FF;" mce_style="background-color:#0099FF;"> <form id="form1" runat="server"> <div style="margin:0px auto 0px auto;" mce_style="margin:0px auto 0px auto;"> <asp:Image ID="Image1" runat="server" BorderStyle="Double" Width="750" ImageAlign="Middle" AlternateText="上传"/> <br /> 上传:<asp:FileUpload ID="UpLoadBorrow" runat="server" Height="22px" onchange="checkFile()" onkeydown="event.returnValue=false;" onpaste="return false" /> <input type="hidden" id="txtFilePath" runat="server" />//这里建立一个隐藏域 用来存地址 <asp:Button ID="btnUpload" runat="server" Text="上传" OnClientClick="return checkNull()" OnClick="btnUpLoad_Click" /> <asp:Button ID="btnBack" runat="server" Text="返回" onmousedown="window.close();" /></div> </form> </body> </html>

 

取值

string inputPath = txtFilePath.Value.Trim();

你可能感兴趣的:(server,File,upload,asp,Path,button)