使用FTP协议上传后文件损坏

时间:2015-02-11 18:49:01

标签: c# ftp wav

我正在尝试使用FTP协议将一些*.wav文件上传到主机。问题是,上传后,文件非常破损!文件的大小与本地文件的大小相同,我可以在损坏的文件中听到一些内容,但是噪音在WAV文件中添加得太多。

这是我用来将文件上传到服务器的代码:

    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

1 个答案:

答案 0 :(得分:3)

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

request.UseBinary = true;