webclient上传文件和附加信息

来源:互联网 发布:淘宝上架宝贝时间限制 编辑:程序博客网 时间:2024/05/17 06:59

   使用WebClient上传,通常用的方法很简单,使用_WebClient.UploadFile(ServerUrl, "post", FilePath)即可,最近做一个项目,上传的文件每次保存的路径不一致,所以就需要上传文件的同时,上传一些附加信息进行判断进而决定保存的位置。

上传页面upload.aspx:

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
try
       
{
            
string FilePath = @"1.log";
            WebClient _WebClient 
= new WebClient();

            NameValueCollection nvcObj 
= new NameValueCollection();
//附加信息
            nvcObj .Add("MyKey""1");
//将附加信息添加至webclient对象
            _WebClient.QueryString = nvcObj ;

            _WebClient.UploadFile(
"http://.../DownloadFile.aspx""post", FilePath);
        }

        
catch (WebException _WebException)
    
{
            Response.Write(_WebException.Message.ToString());
            Response.Write(_WebException.StackTrace );
        }

    }
下载页面DownloadFile.aspx
    protected void Page_Load(object sender, EventArgs e)
    {
        string result = Request.QueryString["MyKey"].ToString();
        foreach (string f in Request.Files.AllKeys)
        {
            HttpPostedFile file = Request.Files[f];
           if(result =="1")
           {
            file.SaveAs("D://uploadfile1//" + file.FileName);
           }
           else
           {
            file.SaveAs("D://uploadfile2//" + file.FileName);
           }
        }   
    }
原创粉丝点击