c# - 使用 FTP 协议(protocol)上传后文件损坏

标签 c# ftp wav

我正在尝试使用 FTP 协议(protocol)将一些 *.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

最佳答案

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

request.UseBinary = true;

关于c# - 使用 FTP 协议(protocol)上传后文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28462110/

相关文章:

ios - iPhone SDK : Play a single WAV from a button

html - 悬停单词时播放wav文件

audio - .wav 文件中的字节代表什么?

c# - 分配变量的成本

c# - 数据绑定(bind)到 asp.net 中的 Bootstrap 表

php - 从 PHP/GD 中的资源获取图像 mimetype?

WordPress 需要 FTP 凭据来更新插件

ftp - 使用 Telnet 传输文件

c# - 字符串到数字生成器

c# - 同一文件中的文本和二进制数据