C#上传类

   using System;
using System.Web.UI;
using System.IO;
namespace wapmanage
{
    
/// <summary>
    
/// Upload 的摘要说明。
    
/// </summary>

    public class Upload : System.Web.UI.Page
    
{
        
public Upload(){}        public string UploadFile(string filePath,int maxSize,string[] fileType,System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
        
{
            
string Result = "UnDefine";
            
bool typeFlag = false;
            
string FilePath = filePath;
            
int MaxSize = maxSize;
            
string strFileName,strNewName,strFilePath;
            
if (TargetFile.PostedFile.FileName=="")
            
{
                
return "FILE_ERR";
            }

            strFileName 
= TargetFile.PostedFile.FileName;
            TargetFile.Accept 
= "*/*";
            strFilePath 
= FilePath;
            
if (Directory.Exists(strFilePath)==false)
            

                Directory.CreateDirectory(strFilePath);
            }

            FileInfo myInfo
=new FileInfo(strFileName);  
            
string strOldName=myInfo.Name;
            strNewName
=strOldName.Substring(strOldName.Length-3,3); 
            strNewName 
= strNewName.ToLower();
            
if( TargetFile.PostedFile.ContentLength <= MaxSize)
            
{
                
for(int i = 0;i<=fileType.GetUpperBound(0);i++)
                
{
                    
if (strNewName.ToLower() == fileType[i].ToString()){typeFlag = true;break;}
                }

                
if(typeFlag)
                
{  
                    
string strFileNameTemp = GetUploadFileName();
                    
string strFilePathTemp = strFilePath ;
                    strOldName 
= strFileNameTemp + "." +strNewName;     
                    strFilePath
=strFilePath + "//" + strOldName;    
                    TargetFile.PostedFile.SaveAs(strFilePath);
                    Result 
= strOldName;
                    TargetFile.Dispose();
                }
else
                
{
                    
return "TYPE_ERR";
                }

            }

            
else
            
{
                
return "SIZE_ERR";
            }

            
return(Result);
        }


        
public string UploadFile(string filePath,int maxSize,string[] fileType, string filename, System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
        
{
            
string Result = "UnDefine";
            
bool typeFlag = false;
            
string FilePath = filePath;
            
int MaxSize = maxSize;
            
string strFileName,strNewName,strFilePath;
            
if (TargetFile.PostedFile.FileName=="")
            
{
                
return "FILE_ERR";
            }

            strFileName 
= TargetFile.PostedFile.FileName;
            TargetFile.Accept 
= "*/*";
            strFilePath 
= FilePath;
            
if (Directory.Exists(strFilePath)==false)
            

                Directory.CreateDirectory(strFilePath);
            }

            FileInfo myInfo
=new FileInfo(strFileName);  
            
string strOldName=myInfo.Name;
            strNewName
=strOldName.Substring(strOldName.Length-3,3); 
            strNewName 
= strNewName.ToLower();
            
if( TargetFile.PostedFile.ContentLength <= MaxSize)
            
{
                
for(int i = 0;i<=fileType.GetUpperBound(0);i++)
                
{
                    
if (strNewName.ToLower() == fileType[i].ToString()){typeFlag = true;break;}
                }

                
if(typeFlag)
                
{  
                    
string strFileNameTemp = filename;
                    
string strFilePathTemp = strFilePath ;
                    strOldName 
= strFileNameTemp + "." +strNewName;     
                    strFilePath
=strFilePath + "//" + strOldName;    
                    TargetFile.PostedFile.SaveAs(strFilePath);
                    Result 
= strOldName;
                    TargetFile.Dispose();
                }

                
else
                
{
                    
return "TYPE_ERR";
                }

            }

            
else
            
{
                
return "SIZE_ERR";
            }

            
return(Result);
        }


        
private string GetUploadFileName()
        
{
            
string Result = "";
            DateTime time 
= DateTime.Now;   
            Result 
+= time.Year.ToString() + FormatNum(time.Month.ToString(),2+ FormatNum(time.Day.ToString(),2+ FormatNum(time.Hour.ToString(),2+ FormatNum(time.Minute.ToString(),2+ FormatNum(time.Second.ToString(),2)  + FormatNum(time.Millisecond.ToString(),3) ;
            
return(Result);
        }


        
public string FormatNum(string Num,int Bit)
        
{            
            
int L;
            L 
= Num.Length;
            
for(int i = L ;i < Bit ;i++)
            
{
                Num 
= "0" + Num;
            }

            
return(Num);
        }


    }

}


 调用:

   //创建给类别的图片保存目录
   var path
    path = "C:/temp/";
    if (!Directory.Exists(path))
    {
     Directory.CreateDirectory(path);
    }
    
    if(this.file.PostedFile.FileName != "")
    {
     string[] filetype = new string[]{"jpg","gif","png","JPG","GIF","PNG"};
     filename = new Upload().UploadFile(path , 204800, filetype, file);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于C#的FTP上传帮助示例: ```csharp using System; using System.IO; using System.Net; public class FtpHelper { private string ftpServerIP; private string ftpUserID; private string ftpPassword; private FtpWebRequest reqFTP; public FtpHelper(string ftpServerIP, string ftpUserID, string ftpPassword) { this.ftpServerIP = ftpServerIP; this.ftpUserID = ftpUserID; this.ftpPassword = ftpPassword; } // 上传文件 public bool Upload(string localFilePath, string remoteFileName) { FileInfo fileInfo = new FileInfo(localFilePath); string uri = "ftp://" + ftpServerIP + "/" + remoteFileName; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.ContentLength = fileInfo.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; using (FileStream fs = fileInfo.OpenRead()) { using (Stream strm = reqFTP.GetRequestStream()) { contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } } } FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); return true; } } ``` 使用方法: ```csharp string localFilePath = @"C:\test.txt"; string remoteFileName = "test.txt"; string ftpServerIP = "ftp://192.168.1.1"; string ftpUserID = "username"; string ftpPassword = "password"; FtpHelper ftpHelper = new FtpHelper(ftpServerIP, ftpUserID, ftpPassword); ftpHelper.Upload(localFilePath, remoteFileName); ``` 其中,`localFilePath`为本地文件路径,`remoteFileName`为远程文件名,`ftpServerIP`为FTP服务器地址,`ftpUserID`和`ftpPassword`为FTP登录凭据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值