上传图片

void btSave_Click(object sender, RoutedEventArgs e)
        {
         
           OpenFileDialog op = new OpenFileDialog(); 

  op.Multiselect = false;
            if(_pvFlag==0)
               op.Filter = "JPG 图片 (*.jpg)|*.jpg|PNG 图片 (*.png)|*.png";
            else
                op.Filter = "Wav 文件(*.wav)|*.wav";            
           if (op.ShowDialog() == true)
           {            
               string filename = op.File.Name;
               string extension = filename.Substring(filename.LastIndexOf('.'));
                uploadName = DateTime.Now.ToString("yyyyMMddhhmmss") + extension;
               this.tbPath.Text = uploadName;

               FileStream fs = op.File.OpenRead();
               UploadUserPhoto(uploadName,fs);          

             //  this.imageShow.Source = new BitmapImage(new Uri("/" + strFoldName + "/" + uploadName, UriKind.Relative));            
             
           }


        }

        private void UploadUserPhoto(string fileName, Stream data)
        {
            string _strSaveFloder = "UploadFile";
            Uri uri = new Uri(string.Format("/WebClientUpload.ashx?fileName={0}&fileFold={1}", fileName, _strSaveFloder), UriKind.Relative);

            WebClient client = new WebClient();
            client.OpenWriteCompleted += (sender, e) =>
            {
                PushData(data, e.Result);
                e.Result.Close();
                data.Close();               

            };
            client.OpenWriteAsync(uri);
        }

        private void PushData(Stream input, Stream output)
        {
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
            {
                output.Write(buffer, 0, bytesRead);
            }
        }

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace test.Web
{
    /// <summary>
    /// Summary description for WebClientUpload
    /// </summary>
    public class WebClientUpload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string fileName = context.Request.QueryString["fileName"].ToString();
            string fileFold = context.Request.QueryString["fileFold"].ToString();

            Stream s = context.Request.InputStream;
            FileStream fs = new FileStream(context.Server.MapPath(fileFold) + "/" + fileName, FileMode.Create);

            byte[] bytes = new byte[s.Length];

            s.Read(bytes, 0, bytes.Length);

            fs.Write(bytes, 0, bytes.Length);

            fs.Flush();

            fs.Close();

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值