不受iis限制的上傳文件的方法

 

class:

// created by kitty 2007-8-2
  
//上傳文件
    public static string UploadFile(byte[] fs, string FileName, string receive)
    
{

        
//如果不存在路徑,則新增文件
        if (!(Directory.Exists(receive)))
        
{
            Directory.CreateDirectory(receive);
        }


        
try
        
{
            MemoryStream m 
= new MemoryStream(fs);
            FileStream f 
= new FileStream(receive + @"/" + FileName, FileMode.Create);

            m.WriteTo(f);
            m.Close();
            f.Close();
            f 
= null;
            m 
= null;

            
return "A";//Success.
        }

        
catch (Exception ee)
        
{
            
return ee.Message.ToString();
        }


    }


調用方法:
  

 //上傳註冊所需文件
    private bool UploadeFile()
    
{
        
//if (Session["hif"] != null)
        
//{
        
//    hif = (ArrayList)Session["dt_cus"];

        
//}

        
//檢查上傳的image文件
        bool result = true;

        
foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
        
{
            
string filepath = "";
            
string filename = "";
            
string filename_old = "";
            
string filetype = "";
            filepath 
= HIF.PostedFile.FileName;
            filename_old 
= filepath.Substring(filepath.LastIndexOf(@"\"+ 1);
            filetype 
= filepath.Substring(filepath.LastIndexOf("."+ 1);
            
string imagestr = DateTime.Now.ToString("yyyyMMddHHmmssfff"); 
           
            filename 
= imagestr + "." + filetype.Trim();

            
//檢查改圖片是否已經存在於文件夾中
            try
            
{
                
byte[] b = new byte[HIF.PostedFile.ContentLength];
                System.IO.Stream fs;

                
string path = @"e:/test";//Server.MapPath(@"../file") ;//保存的文件路徑

                
if (File.Exists(path.Trim()))
                
{
                    lb_msg.Text 
= "該圖片已經存在";
                    
//result = false;
                }

                
else
                
{
                   
// HIF.PostedFile.SaveAs(path.Trim());//保存圖片倒指定文件夾

                    fs 
= (System.IO.Stream)HIF.PostedFile.InputStream;

                    fs.Read(b, 
0, HIF.PostedFile.ContentLength);

                    
string re = fn_share.UploadFile(b, filename, path);

                    fs.Close();

                    
if (re.Trim() == "A")
                    
{
                        FileName.Value 
+= filename_old + "/";
                        FilePath.Value 
+= filename + "/";
                    }

                    
else
                    
{
                        lb_msg.Text 
+= "上傳文件出錯:" + re;
                        result 
= false;
                    }

                   
                }


            }

            
catch (Exception ex)
            
{
                lb_msg.Text 
+= "上傳文件出錯:" + ex.Message;
                result 
= false;
            }


        }

        
return result;



    }

posted @ 2007-08-30 17:37  Nina  阅读(308)  评论(0编辑  收藏  举报