WCF File Upload

Source: Internet
Author: User

How to upload files on the WCF Client

/// <Summary> /// upload a file /// </Summary> /// <Param name = "userid"> used for verification </param> /// <param name = "extension"> extension </param> // <Param name = "shuzhu"> This is the byte array data of the file </param> // <Param name = "dbmd5"> test data </param> /// <Param name = "oldphotoname"> you can delete a previous file when uploading duplicate data. </param> /// <returns> </returns> Public String filesupload (string userid, string extension, byte [] shuzhu, string dbmd5, string oldphotoname) {string fa Llies = ""; 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) {// delete the file if the old image exists. delete (temps + "\" + oldphotoname) ;}} catch {}if (string. isnullorempty (oldphotoname. trim () {random RND = new random (); oldphotoname = datetime. now. tostring ("yyyymmddhhmmss") + RND. next (1000,999 9);} fallies = temps + "\" + oldphotoname + ". "+ extension; If (! Directory. exists (temps) directory. createdirectory (temps); file. writeallbytes (fallies, shuzhu); fallies = "files/" + oldphotoname + ". "+ extension ;}} catch {fallies =" ";}} return fallies ;}

Using the system. Io file. writeallbytes () method can save a lot of trouble.

The following is the code uploaded by the client.

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; // create an object stream sr = hp. inputstream; // create a data stream object byte [] B = new byte [fileuplaods. postedfile. contentlength]; // defines the byte array Sr. read (B, 0, fileuplaods. postedfile. contentlength); // place the image data in the B array object instance. 0 indicates the starting position of the array pointer, and uplength indicates the length of the stream to be read (the end position of the pointer) Sr. close (); Sr. dispose (); try {string accinfo = userinfocookie. usercookieinfo (). s Trsysid; // The following describes how to dynamically create a WCF Service.: 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, "File Upload Failed. ") ;}} Else {MessageBox. Show (this. Page," the file format is incorrect. ") ;}} Catch (system. exception ex) {MessageBox. show (this. page, "file:" + ex. message); return ;}} else {MessageBox. show (this. page, "select file") ;}} else {MessageBox. show (this. page, "the file is too large ");}}

Finally, let's take a look at how the WCF server configuration file is configured.

<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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.