Upload and download a song file (C #)

Source: Internet
Author: User

During the first project training, I made a Silverlight-based Bubble karaoke system. I am responsible for the Administrator part (which is similar to a client and written in WPF ), in the meantime, the function of uploading and downloading songs is used.

I found a lot of source code for song upload and download on the Internet, but they mainly talk about file upload and download, while some of the cases are images or other files, when it comes to specific song files, few files are uploaded. In addition, the upload and download of song files are different from those of other files. I have studied it for a day, and I have a general understanding of it.

Now I am posting the code I have written, hoping to help you.

(It is uploaded as a corresponding btton event, so it is like this, there are some service classes in the middle, used to add records in the database, etc)

Upload:

Private void uploadsongbtn_click (Object sender, routedeventargs e) {// select the Upload File to upload to the destination directory ..... // The database does not exist now... Musicfileinfo = new fileinfo (filepathtxt. text); uploadfileinfo = new uploadfileinfo (); uploadfileinfo. name = musicfileinfo. name; stream = musicfileinfo. openread (); uploadfileinfo. size = (INT) (stream. length/1024); uploadfileinfo. data = new list <byte []> (); int Len; long rest; while (stream. position>-1 & stream. position <stream. length) {rest = stream. length-stream. posit Ion; Len = (rest> 16384 )? 16384: (INT) rest; byte [] filedata = new byte [Len]; stream. read (filedata, 0, Len); uploadfileinfo. data. add (filedata);} stream. close (); progressbar1.maximum = uploadfileinfo. data. count; progressbar1.value = 0; uploadfileservice. uploadfileserviceclient client = new uploadfileservice. uploadfileserviceclient (); bool isappend = false; For (INT I = 0; I <uploadfileinfo. data. count; I ++) {client. douploadfile (musicfileinfo. name, uploadfileinfo. data [I], isappend); progressbar1.value ++; isappend = true ;}}

Download:

Private void downloadservice_downloadfilecompleted (Object sender, downloadservicereference. downloadfilecompletedeventargs e) {If (E. error = NULL) {savefiledialog savedialog = (savefiledialog) E. userstate; // obtain the savefiledialog using (Stream stream = savedialog. openfile () {stream. write (E. result, 0, E. result. length);} MessageBox. show ("Download successful! ");} Else {MessageBox. Show (" Download failed! ");}}

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.