winform上传文件

//上传图片 文件
public int addUpPic( String strProCode,String strFileName,String strUpType)
{
//strFileName 旧路径
String strPicName = Path.GetFileName(strFileName);
//// new BasicClass().splitFileName(strFileName);
//新路径
String path = "";
//备份文件夹
String backPath = "";

       if (strUpType == "文件")
       {
           path = new DateBasicInfo.ConDb().getParamter()[3] + @"" + strPicName;
            backPath = new DateBasicInfo.ConDb().getParamter()[4]; 
       }
       else if (strUpType == "图纸")
       {
           path = new DateBasicInfo.ConDb().getParamter()[6] + @"" + strPicName;
           backPath = new DateBasicInfo.ConDb().getParamter()[7];
       }
       else if (strUpType == "样品")
       {
           path = new DateBasicInfo.ConDb().getParamter()[8] + @"" + strPicName;
           backPath = new DateBasicInfo.ConDb().getParamter()[9];
       } 
       else {
           //图片
           path = new DateBasicInfo.ConDb().getParamter()[1] + @"" + strPicName;
           backPath = new DateBasicInfo.ConDb().getParamter()[2]; 
       }
    

       //备份文件名
       String backFileName=getFile( backPath,strFileName);

       try
       {
           try
           {
               if (File.Exists(path))
               {
                   //如果已经上传过,将上次上传的文件复制到proImageBack  文件夹下
                   File.Copy(path, backFileName, true);
               }
             
           }catch(Exception  ee)
           {
               System.Windows.Forms.MessageBox.Show("不存在文件"+path);
               return 0;
           }

           //上传文件
           File.Copy(strFileName.Trim(), path, true);

           SqlParameter[] parms ={ 
             new SqlParameter("@pro_code",strProCode),
             new SqlParameter("@assistantName",strPicName),
             new SqlParameter("@flag","1"),
             new SqlParameter("@upType",strUpType),
               new SqlParameter("@upMan",DateBasicInfo.userMessage.userName)
               
           
        };

           return DateBasicInfo.ConDb.ExecProc("addUpProAssistant", parms);

       }
       catch (Exception ex)
       {
           return 0;
       }

   }

// System.Diagnostics.Process.Start(strFilePath); 打开文件

/////下载
private void btnDownload_Click(object sender, EventArgs e)
{
String strUpType = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "anType").ToString();
String strFile = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "annexName").ToString();
String strFilePath = "";
if (strUpType == "图片")
{
strFilePath = new DateBasicInfo.ConDb().getParamter()[1] + @"" + strFile;
}
else if (strUpType == "图纸")
{
strFilePath = new DateBasicInfo.ConDb().getParamter()[6] + @"" + strFile;

        }
        else if (strUpType == "样品")
        {

            strFilePath = new DateBasicInfo.ConDb().getParamter()[8] + @"" + strFile;
        }
        else
        {
            strFilePath = new DateBasicInfo.ConDb().getParamter()[3] + @"" + strFile;
        }

        #region 本地文件拷贝
        //OpenFileDialog filedlg = new OpenFileDialog();  
        //filedlg.Multiselect = false;
        //string strFilePath="";
        //if (filedlg.ShowDialog() == DialogResult.OK)
        //{
        //    strFilePath = filedlg.FileName;

        //}
        //else
        //{
        //    return;
        //} 
        #endregion
        FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
        byte[] bt = new byte[fs.Length];
        fs.Position = 0;
        fs.Read(bt, 0, Convert.ToInt32(fs.Length));
        fs.Close();


         FolderBrowserDialog fld = new FolderBrowserDialog();
         if (fld.ShowDialog() == DialogResult.OK)
         {
             try
             {
                 string path = fld.SelectedPath;
                 if (!path.EndsWith(@""))//判斷是否為根目錄,如果不是根目錄要添加分隔符""  
                     path += @"";

                 byte[] btFile = bt;
                 int ind=strFilePath.LastIndexOf('\')+1;
                 FileStream fss = new FileStream(path + strFilePath.Substring(ind, strFilePath.Length - ind), FileMode.Create);
                 fss.Write(btFile, 0, btFile.Length);
                 fss.Close();

             }
             finally
             {

             }
         }

    }
原文地址:https://www.cnblogs.com/VictorBlog/p/5818046.html