WPF uploading files to the server

Source: Internet
Author: User

Create an empty Web site, create a uploadfile.aspx item,

When the server reports a 500 error, check to see if the file save path exists

1     protected voidPage_Load (Objectsender, EventArgs e)2        {3            foreach(stringFinchRequest.Files.AllKeys)4{//to pass in a new file on the client5Httppostedfile file =Request.files[f];6                //A new file name is passed in to the client7                stringDirectory = request.querystring["D"];8                stringfilename = request.querystring["N"];9                //file. SaveAs (Server.MapPath (". /reportfile/"+ filename + file. Filename.substring (file. Filename.indexof (".") ));Ten                stringPath =string. Format (@"g:\reportfile\{0}\", directory); One                if(!directory.exists (path)) {directory.createdirectory (path);} AFile. SaveAs (path +filename); -            } -}

In WPF

1 usingform =System.Windows.Forms;2         Private Const stringDirectory ="0123";3          Public voidUpload (stringfile)4         {5FileInfo info =NewFileInfo (file);6             stringURL =string. Format ("Http://192.168.31.118:54040/uploadfile.aspx?d={0}&n={1}", directory, info. Name);7WebClient client =NewWebClient ();8Client. Credentials = CredentialCache.DefaultCredentials;//Gets or sets the network credentials that are sent to the host and used for authentication requests9Client. Uploadfileasync (NewUri (URL), file);TenClient. uploadfilecompleted + =NewUploadfilecompletedeventhandler (result_uploadfilecompleted); One         } A         Private voidResult_uploadfilecompleted (Objectsender, UploadFileCompletedEventArgs e) -         { -             if(E.error! =NULL) the             { -MessageBox.Show ("Upload failed:"+e.error.message); -             } -             Else +             { -MessageBox.Show ("Upload Success! "); +             } A         } at  -         Private voidUploadfile_click (Objectsender, RoutedEventArgs e) -         { -Form. OpenFileDialog _dialog =Newform. OpenFileDialog (); -_dialog. MultiSelect =true; -             if(_dialog. ShowDialog () = =form. DialogResult.OK) in             { -                 string[] _files =_dialog. FileNames; to                 if(_files! =NULL&& _files. Length >0) +                 { -                     foreach(varIteminch_files) the                     { * Upload (item); $                     }Panax Notoginseng                 } -             } the}

WPF uploading files to the server

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.