WCF 文件上传

这个WCF端上传文件方法

    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="userID">验证用的</param>
    /// <param name="Extension">扩展名</param>
    /// <param name="shuzhu">这是文件的byte数组数据</param>
    /// <param name="DBMD5">对数据检验</param>
    /// <param name="oldPhotoName">上传重复数据时可以删除之前的文件</param>
    /// <returns></returns>
    public string FilesUpLoad(string userID, string Extension, byte[] shuzhu, string DBMD5, string oldPhotoName)
    {
        string fallies = "";
        string msg = "";
        if (UNIC.Common.Public.CheckDBMD5(userID, DBMD5, "DBPassWord", out msg))
        {
            try
            {

                if (!Public.getXmlElementValue("NotPass").ToString().Trim().Contains(Extension))
                {
                    string temps = System.Web.HttpContext.Current.Server.MapPath("Files");
                    try
                    {

                        if (!string.IsNullOrEmpty(oldPhotoName))
                        {
                            if (File.Exists(temps + "\\" + oldPhotoName))
                            {
                                //判断旧图片是否存在,存在就删除
                                File.Delete(temps + "\\" + oldPhotoName);
                            }
                        }
                    }
                    catch
                    {

                    
                    }
                    if (string.IsNullOrEmpty(oldPhotoName.Trim()))
                    {
                        Random rnd = new Random();
                        oldPhotoName = DateTime.Now.ToString("yyyyMMddHHmmss") + rnd.Next(1000, 9999);
                    }
                    fallies = temps + "\\" + oldPhotoName + "." + Extension;
                    if (!Directory.Exists(temps))
                        Directory.CreateDirectory(temps);

                    File.WriteAllBytes(fallies, shuzhu);
                    fallies = "Files/" + oldPhotoName + "." + Extension;
                }
            }
            catch
            {
                fallies = "";
              
            }
        }
        return fallies;
    }

使用 system.io File.WriteAllBytes()这个方法,可以省掉很多麻烦

下面是客户端上传部分的代码


   if (FileUpLaods.HasFile)
            {

                if (FileUpLaods.FileContent.Length <= int.Parse(Public.getXmlElementValue("PhotoMaxLg").ToString()))
                {
                    string fileName = FileUpLaods.PostedFile.FileName;
                    System.IO.Path.GetFileNameWithoutExtension(FileUpLaods.FileName);

                    string fileLocal = System.IO.Path.GetFullPath(FileUpLaods.PostedFile.FileName);
                    if (fileLocal.Length > 0)
                    {
                        string extraName = System.IO.Path.GetExtension(FileUpLaods.PostedFile.FileName).Replace(".", "");
                        try
                        {
                            string extstr = Public.getXmlElementValue("NotPass");
                            if (!extstr.Contains(extraName.ToLower()))
                            {

                                HttpPostedFile hp = FileUpLaods.PostedFile;//创建访问客户端上传文件的对象    
                                Stream sr = hp.InputStream;//创建数据流对象    
                                byte[] b = new byte[FileUpLaods.PostedFile.ContentLength];//定义byte型数组 
                                sr.Read(b, 0, FileUpLaods.PostedFile.ContentLength);//将图片数据放到b数组对象实例中,其中0代表数组指针的起始位置,uplength表示要读取流的长度(指针的结束位置)
                                sr.Close();
                                sr.Dispose();
                                try
                                {
                                    string accinfo = UserInfoCookie.UserCookieInfo().strSysID;//下面是动态创建WCF服务。:
                                    IFileUpLoadServ FileServ = InvokeContext.CreateWCFServiceByURL<IFileUpLoadServ>(Public.getXmlElementValue("LocalDpFileUploadServ"), "basicHttpBinding");
                                    savefilepacht = FileServ.FilesUpLoad(accinfo, extraName, b,
                                        DEncrypt.Encrypt(accinfo, Public.getXmlElementValue("DBPassWord")), "");

                                }
                                catch 
                                {
                                    MessageBox.Show(this.Page, "文件上传失败。");

                                }


                            }
                            else
                            {
                                MessageBox.Show(this.Page, "文件格式不对。");
                            }
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(this.Page, "文件:" + ex.Message);
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show(this.Page, "请选择文件");
                    }
                }
                else
                {
                    MessageBox.Show(this.Page, "文件太大");
                }
            }


最后看看,WCF服务端配置文件怎么配置的

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IFileUpLoadServ" maxReceivedMessageSize="20971510">
          <readerQuotas maxStringContentLength="20971520" maxArrayLength="20971520"/>
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="MyBehavior" name="FileUpLoadServ">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileUpLoadServ" contract="FilesServices.IFileUpLoadServ"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值