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! ");}}