net上传文件到服务器|aspnet中文件如何上传到服务器上

|

❶ asp.net中上传文件到远程FTP服务器指定目录下,求大神帮助,小弟不胜感激

private string ftpServerIP = "服务器ip";//服务器ipprivate string ftpUserID = "ftp的用户名";//用户名private string ftpPassword = "ftp的密码";//密码//filename 为本地文件的绝对路径//serverDir为服务器上的目录private void Upload(string filename,string serverDir) { FileInfo fileInf = new FileInfo(filename); string uri = string.Format("ftp://{0}/{1}/{2}", ftpServerIP,serverDir,fileInf.Name); FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); // ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型 reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为2kb int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; // 打开一个文件流 (System.IO.FileStream) 去读上传的文件 FileStream fs = fileInf.OpenRead(); try { // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的2kb contentLen = fs.Read(buff, 0, buffLength); // 流内容没有结束 while (contentLen != 0) { // 把内容从file stream 写入 upload stream strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } // 关闭两个流 strm.Close(); fs.Close(); } catch (Exception ex) { // MessageBox.Show(ex.Message, "Upload Error"); Response.Write("Upload Error:" + ex.Message); } }调用方法string filename = "D:\\test.txt"; //本地文件,需要上传的文件string serverDir = "img"; //上传到服务器的目录,必须存在Upload(filename,serverDir);

❷ ASP.net在上传文件时,如何让其上传到其它服务器磁盘上

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="gb2312" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <%@ Import Namespace="System" %> <%@ Import Namespace="System.Collections" %> <%@ Import NameSpace="System.Web" %> <script language="VB" runat="server"> sub page_load(Sender As Object, e As EventArgs) if IsPostBack Then save_files() end if end sub sub save_files() dim files as HttpFileCollection =HttpContext.Current.Request.Files dim strMsg as System.Text.StringBuilder =new System.Text.StringBuilder() strMsg.Append("上传的文件分别是:<hr color=red>") dim fileno as integer try for fileno=0 to files.count-1 dim postedfile as HttpPostedFile=files(fileno) dim filename,fileext as string filename=System.IO.Path.GetFileName(postedFile.FileName) if filename<>"" then fileext = System.IO.Path.GetExtension(fileName) strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>") strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>") strMsg.Append("上传文件的文件名:" + fileName + "<br>") strMsg.Append("上传文件的扩展名:" + fileext + "<br><hr>") postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("/temp2/") + fileName) <!– 注意: temp2是图片上传的位置–> end if next fileno strStatus.Text = strMsg.ToString() catch Ex as System.Exception strStatus.Text = Ex.Message end try end sub </script> <HTML> <HEAD> <title>多文件上传</title> <script language="JavaScript"> function addFile() { var str = '<INPUT type="file" size="50" NAME="File">' document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str) } </script> </HEAD> <body> <form id="form1" method="post" runat="server" enctype="multipart/form-data"> <div align="center"> <h3>ASP.NET多文件上传</h3> <P id="MyFile"><INPUT type="file" size="50" NAME="File"></P> <P> <input type="button" value="增加(Add)" onClick="addFile()"> <input onClick="this.form.reset()" type="button" value="重置(ReSet)"> <asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button> </P> <P> <asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt" Width="500px" BorderStyle="None" BorderColor="White"></asp:Label> </P> </div> </form> </body> </HTML>

❸ asp.net上传文件到服务器指定文件夹问题

#region 文件上传(普通上传不生成文件夹) /// <summary> /// 文件上传(普通上传不生成文件夹) /// </summary> /// <param name="hifile">上传控件</param> /丛岩纯// <param name="strAbsolutePath">绝对路径</param> /// <param name="枣腊TYPE">文件类型(在WebConfig中配置)</param> /// <param name="FileSize">文件大小单位:Mb(在WebConfig中配置)</param> /// <returns></returns> public string SaveFile(HtmlInputFile hifile, string strAbsolutePath, string TYPE, string FileSize) { bool filetype = false; string /*文件的完整路径*/strOldFilePath = "", /*文件类型*/strExtension = "", /*新文件名称*/strNewFileName = ""; //如果上传文件的文件名不为空 if (hifile.PostedFile.FileName != string.Empty) { strOldFilePath = hifile.PostedFile.FileName;//文件的完整路径 Path = strAbsolutePath;//服务器路径 //取得上传文件的扩展名 strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."渗咐) + 1); file_type = strExtension; //文件大小单位Mb double sizes = hifile.PostedFile.ContentLength / (double)1024.0 / (double)1024.0;//文件大小 this.file_size = sizes.ToString("f4");//文件大小单位Mb精确到小数点后4位 //文件格式 string[] types = TYPE.Split('|');//文件格式集合 for (int i = 0; i < types.Length; i++) { if (types[i].ToString().Trim().ToLower() == strExtension.Trim().ToLower()) { filetype = true; break; } else { filetype = false; } } if (filetype)//判断文件类型是否错误 { if (sizes < Convert.ToDouble(FileSize)) { //文件上传后的命名 strNewFileName = GetUniqueString() + "." + strExtension; file_name = strNewFileName; if (!Directory.Exists(strAbsolutePath))//文件的完整路径 { Directory.CreateDirectory(strAbsolutePath);//按月份创建文件夹 } hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);//保存文件 return "文件上传成功!" + file_name; } else { return "文件大小不能超过" + FileSize + ".00Mb!"; } } else { return "文件格式错误!"; } } else { return "请选择要上传的文件!"; } }

❹ .net把任意文件的路径保存到sql数据库中,并把对应文件上传到服务器 代码怎么实现

你搜下.net上传文件,最后会File.saveAs(文件路径),这样就会把文件上传到你想存放的位置,文件路径你再存储到数据库中都可以了

❺ .net上传EXCEL文件到服务器导入数据库,成功上传,但是导入时找不到文件,是不是导入时路径有问题

从代码来看,是先将上传的文件保存之后,使用Excelds方法来读取Excel文件中的数据,上传保存文件和读取数据的代码是在同一个代码段之中执行的,并且有提到“文件能在服务器上找到”,说明上传的文件有成功保存,故可以排除savePath值的问题。那么问题应该是读取数据这边的,即Excelds的问题了。从代码来看,Excelds有两个参数,目前是传入了savePath和filename,从此参数的结构来看应该是传入一个不含文件名的路径和一个文件名。但是,这里传入的savePath已经包含了文件名(第一个语句中有加入filename),所有猜测问题应该是传给Excelds的路径有问题,不应该包含文件名。

❻ asp.net中文件如何上传到服务器上

{ } protected void Button1_Click(object sender, EventArgs e){if (this.filepost.PostedFile.FileName == ""){Response.Write("上传文件不能为空!");return;}try{string Path = Server.MapPath("upload/");//设置服务器端路径string filePath = this.filepost.PostedFile.FileName;//获取客户端实际路径string fileName = filePath.Substring(filePath.LastIndexOf("\\")+1);//获取文件名称string serverPath = Path + fileName;//上传的文件保存到服务器端的路径System.Text.StringBuilder buider = new System.Text.StringBuilder();//上传的文件信息:可变字符串buider.Append("上传文件的类型:"+this.filepost.PostedFile.ContentType.ToString()+"");buider.Append("客户端文件地址:"+this.filepost.PostedFile.FileName+"");buider.Append("上传文件名称:"+fileName);buider.Append("上传文件的扩展名:"+filePath.Substring(fileName.LastIndexOf(".")+1));buider.Append("上传文件的大小:"+this.filepost.PostedFile.ContentLength/1024+"k"+"");if (System.IO.File.Exists(serverPath)){Response.Write("这个文件在服务器上已经存在,请不要重复上传!");return;}string str = fileName.Substring(fileName.LastIndexOf(".")+1);//获取文件后缀名if (str == "jpg" || str == "rar" || str == "ppt"){Response.Write("对不起,该类型文件不能被上传!");return;}if (this.filepost.PostedFile.ContentLength >2048){Response.Write("对不起,文件不能超过2K");return;}this.filepost.PostedFile.SaveAs(serverPath);//上传保存文件


赞 (0)