[英]File corrupts afters uploading using FTP protocol
我正在嘗試使用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);
}
這是上傳前的一些文件:
這是上傳后的文件:
您必須將您的Ftp請求設置為二進制request.UseBinary
為true
。
request.UseBinary = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.