webclient实现多图片上传

来源:互联网 发布:科立捷对讲机端口是几 编辑:程序博客网 时间:2024/04/27 21:34

 using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ar = Server.MapPath("~/Image")+"/";
    }
    private static string ar = "";
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            Invoker ins = new Invoker();
            ins.Invoke();
            ins.InvokeMethod();
            Response.Write("成功");
        }
        catch
        { }
    

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
     
    }
    public class Invoker
    {
        public OpenFileDialog InvokeDialog;
        private Thread InvokeThread;
        private DialogResult InvokeResult;
    
        public Invoker()
        {
            InvokeDialog = new OpenFileDialog();
            InvokeDialog.Multiselect = true;
            InvokeDialog.Filter = "图片(*.gif;*.jpg;*.jpeg;*.jnp;*.bmp)|*.gif;*.jpg;*.jpeg;*.jnp;*.bmp|所有文件(*.*)|*.*";
        
            InvokeThread = new Thread(new ThreadStart(InvokeMethod));
            InvokeThread.SetApartmentState(ApartmentState.STA);
            InvokeResult = DialogResult.None;
        }

        public DialogResult Invoke()
        {
            InvokeThread.Start();
            InvokeThread.Join();
            return InvokeResult;
        }

        public void InvokeMethod()
        {
            InvokeResult = InvokeDialog.ShowDialog();

        
            foreach (string str in InvokeDialog.FileNames)
            {
                UpLoadFile1(str, ar);
             
            }

        }
    }


    /// <summary>
    /// WebClient上传文件至服务器
    /// </summary>
    /// <param name="fileNamePath">文件名,全路径格式</param>
    /// <param name="uriString">服务器文件夹路径</param>
    private static  void UpLoadFile1(string fileNamePath, string uriString)
    {
        try
        {
            string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("//") + 1);
            string Name = fileName.Substring(0, fileName.LastIndexOf("."));
            string NewFileName = Name+""+ fileNamePath.Substring(fileNamePath.LastIndexOf("."));
            string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);

            if (fileNameExt == "gif" || fileNameExt == "jpg" || fileNameExt == "jpeg" || fileNameExt == "jnp" || fileNameExt == "bmp" || fileNameExt == "GIF" || fileNameExt == "JPG" || fileNameExt == "JPEG" || fileNameExt == "JNP" || fileNameExt == "BMP")
            {
                if (uriString.EndsWith("/") == false)
                {
                    uriString = uriString + "/";
                }
               uriString = uriString + NewFileName;

                // 创建WebClient实例
                WebClient myWebClient = new WebClient();
                myWebClient.Credentials = CredentialCache.DefaultCredentials;

                // 要上传的文件
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
                try
                {
                    //使用UploadFile方法可以用下面的格式               
                    byte[] postArray = r.ReadBytes((int)fs.Length);
                    int i = postArray.Length;
                    if (i / 1024 < 512)
                    {
                        Stream postStream = myWebClient.OpenWrite(uriString, "PUT");

                        if (postStream.CanWrite)
                        {
                            postStream.Write(postArray, 0, postArray.Length);
                        }
                        else
                        {
                          
                        }
                        postStream.Close();
                    }
                    else
                    {
                      
                    }
                }
                catch
                {
                 
                }
            }
            else
            {
              }

        }
        catch
        {
          }
    }

}

原创粉丝点击