2015-02-11 57 views
1

我试图使用FTP协议将一些* .wav文件上传到主机。但事情是,上传后的文件非常破坏!文件的大小与本地文件相同,我可以在损坏的文件中听到某些内容,但噪声被添加到* .wav文件中。 这是他的代码我用我的文件上传到服务器:使用FTP协议上传后文件损坏

public void UploadViaFtp(Object fn) 
    { 
     string filename = (string)fn; 
     // Get the object used to communicate with the server. 
     FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.****.com/htdocs/uploads/" + filename + ".wav"); 
     request.Method = WebRequestMethods.Ftp.UploadFile; 

     // This example assumes the FTP site uses anonymous logon. 
     request.Credentials = new NetworkCredential("****", "****"); 

     // Copy the contents of the file to the request stream. 
     StreamReader sourceStream = new StreamReader(filename + ".wav"); 
     byte[] fileContents = Encoding.Default.GetBytes(sourceStream.ReadToEnd()); 
     sourceStream.Close(); 
     request.ContentLength = fileContents.Length; 

     Stream requestStream = request.GetRequestStream(); 
     requestStream.Write(fileContents, 0, fileContents.Length); 
     requestStream.Close(); 

     FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

     this.Dispatcher.Invoke((Action)(() => box.Text += "\nFile \"" + filename + "\" Uploaded Successfully!!!")); 

     response.Close(); 
    } 

,如果它是有用的,这是记录声音文件我的代码:

[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] 
    private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback); 

    struct RecUploadStruct 
    { 
     public DateTime timeForNaming; 
    } 

    public void RecUpload(Object param) 
    { 
     RecUploadStruct iParam = (RecUploadStruct)param; 

     mciSendString("open new Type waveaudio Alias recsound", "", 0, 0); 
     mciSendString("record recsound", "", 0, 0); 
     Console.WriteLine("recording, press Enter to stop and save ..."); 

     Thread.Sleep(1000); 

     string name = iParam.timeForNaming.Day.ToString() + iParam.timeForNaming.Hour.ToString() + iParam.timeForNaming.Minute.ToString() + iParam.timeForNaming.Second.ToString(); 

     mciSendString("save recsound " + name + ".wav", "", 0, 0); 
     mciSendString("close recsound ", "", 0, 0); 

     Thread uploader = new Thread(UploadViaFtp); 

     uploader.Start(name); 
    } 

,这是某些文件上传前: before

,这是上传后的文件: after

请帮助我解决任何问题或建议。 谢谢。

+0

你可能不应该在代码中包含真正的凭据。 – 2015-02-11 18:52:50

+2

是否http://stackoverflow.com/questions/12650013/c-sharp-file-is-corrupt-after-uploaded-to-server帮助? – KenD 2015-02-11 18:54:12

+1

谢谢@ReticulatedSpline。 – ConductedClever 2015-02-11 18:55:45

回答

3

您必须将您的Ftp请求设置为二进制request.UseBinarytrue

request.UseBinary = true; 
+0

我检查过。它没用! – ConductedClever 2015-02-11 19:01:54

+0

@ConductedClever为什么不呢? – Jens 2015-02-11 19:02:31

+2

声音仍然很嘈杂。如果你想我给你的声音! – ConductedClever 2015-02-11 19:04:54