gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:42:51 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com