简体   繁体   中英

File corrupts afters uploading using FTP protocol

I am trying to upload some *.wav files to a host using FTP protocol. The thing is that, after uploading, the file is very damaged! The size of file is the same as the file on local and I can hear something in the corrupted file but noise is added too much to the WAV file.

This is the code I use to upload my files to server:

    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();
    }

This is the code that records sound files for me:

    [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);
    }

and this is some file before uploading:

before

and this is the file after uploading:

after

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

request.UseBinary = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM