• asp.net C#如何上传指定路径的图片,不用FileUpload控件[转]


    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Web.UI.HtmlControls; 
    /// <summary> 
    /// MyUpload 的摘要说明 
    /// </summary> 
    public class MyUpload 

        private System.Web.HttpPostedFile postedFile = null; 
        private string savePath = ""; 
        private string[] extension; 
        private int fileLength = 0; 
        public string[] filestype ={ "txt", "doc", "gif" }; 
    public MyUpload() 

      // 
      // TODO: 在此处添加构造函数逻辑 
      // 

        public string Help 
        { 
            get 
            { 
                string helpstring; 
                helpstring = " <font size=3>MyUpload myUpload=new MyUpload(); //构造函数"; 
                helpstring += "myUpload.PostedFile=file1.PostedFile;//设置要上传的文件"; 
                helpstring += "myUpload.SavePath=\"e:\\\";//设置要上传到服务器的路径,默认c:\\"; 
                helpstring += "myUpload.FileLength=100; //设置上传文件的最大长度,单位k,默认1k"; 
                helpstring += "myUpload.Extension=\"doc\";设置上传文件的扩展名,默认txt"; 
                helpstring += "label1.Text=myUpload.Upload();//开始上传,并显示上传结果"; 
                helpstring += " <font size=3 color=red>"; 
                return helpstring; 
            } 
        } 
        public System.Web.HttpPostedFile PostedFile 
        { 
            get 
            { 
                return postedFile; 
            } 
            set 
            { 
                postedFile = value; 
            } 
        } 
        public string SavePath 
        { 
            get 
            { 
                if (savePath != "") return savePath; 
                return "c:\\"; 
            } 
            set 
            { 
                savePath = HttpContext.Current.Server.MapPath("../upimages/") + value;/////////////////////注意这里选取上传文件路径,可以修改成传递参数类型的 
            } 
        } 
        public int FileLength 
        { 
            get 
            { 
                if (fileLength != 0) return fileLength; 
                return 1024; 
            } 
            set 
            { 
                fileLength = value * 1024; 
            } 
        } 
        public string[] Extension 
        { 
            get 
            { 
                if (extension != null) return extension; 
                return filestype; 
            } 
            set 
            { 
                extension = value; 
            } 
        } 
        public string PathToName(string path) 
        { 
            int pos = path.LastIndexOf("\\"); 
            return path.Substring(pos + 1); 
        } 
        public string Upload() 
        { 
            int biaoshi = 0; 
            if (PostedFile.FileName != "") 
            { 
                try 
                { 
                    string fileName = PathToName(PostedFile.FileName); 
                    for (int i = 0; i < Extension.Length; i++) 
                    { 
                        if (fileName.EndsWith(Extension[i])) 
                        { 
                            biaoshi = 1; 
                        } 
                    } 
                    if (biaoshi != 1) 
                    { 
                        string errs = ""; 
                        for (int i = 0; i < Extension.Length; i++) 
                        { 
                            errs = errs + Extension[i] + "/"; 
                        } 
                        return "您必须选择" + errs + "类型的文件!"; 
                    } 
                    if (PostedFile.ContentLength > FileLength) return "文件太大,超过了限定值!"; 
                    PostedFile.SaveAs(SavePath + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4)); 
                    return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileName.Substring(fileName.Length - 4, 4); 
                } 
                catch (System.Exception exc) { return exc.Message; } 
            } 
            return "请选择文件后再进行上传!"; 
        } 

    ////////////////////使用例子,直接复制没有修改///////////////////// 
            string[] filestype ={ "gif", "jpg", "png" }; 
            MyUpload myload = new MyUpload(); 
            myload.PostedFile = File1.PostedFile; 
            myload.SavePath = ""; 
            myload.FileLength = 1000; 
            myload.Extension = filestype; 
            Label1.Text = myload.Upload(); 
            if (Label1.Text.StartsWith("200")) 
            { 
                Label2.Text = "upimages/" + Label1.Text; 
                Label1.Text = "上传更新成功!"; 
            }
       
    通过Ftp上传文件 
    public static void UploadToFtp(string fileName ) 
            { 
                FileInfo fileInf = new FileInfo(fileName); 
                FtpWebRequest reqFTP; 
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(@"ftp address" + "/" + fileName)); 
                reqFTP.Credentials = new NetworkCredential("user", "password"); 
                reqFTP.KeepAlive = false; 
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
                reqFTP.UseBinary = true; 
                reqFTP.ContentLength = fileInf.Length; 
              int buffLength = 2048; 
              byte[] buff = new byte[buffLength]; 
              int contentLen; 
                FileStream fs = fileInf.OpenRead(); 
                Stream strm = reqFTP.GetRequestStream(); 
              contentLen = fs.Read(buff, 0, buffLength); 
                while (contentLen != 0) 
                { 
                    strm.Write(buff, 0, contentLen); 
                    contentLen = fs.Read(buff, 0, buffLength); 
                } 
                strm.Close(); 
              fs.Close(); 
            }
  • 相关阅读:
    git 的分支体系命令汇总
    git命令行学习思路总结
    angular1.5版本的自我认识
    我最想去的公司啊 -- 幸福面试两小时
    【转】【Asp.Net MVC】asp.net mvc Model验证总结及常用正则表达式
    时间被序列化后的页面显示的问题
    JS组件Bootstrap实现弹出框和提示框效果代码
    Bootstrap组件之导航条
    Bootstrap学习笔记(四)-----Bootstrap每天必学之表单
    Bootstrap学习笔记(三)-----Bootstrap每天必学之表格
  • 原文地址:https://www.cnblogs.com/tongdengquan/p/6090534.html
Copyright © 2020-2023  润新知