我試圖使用FTP協議將一些* .wav文件上傳到主機。但事情是,上傳後的文件非常破壞!文件的大小與本地文件相同,我可以在損壞的文件中聽到某些內容,但噪聲被添加到* .wav文件中。 這是他的代碼我用我的文件上傳到服務器:使用FTP協議上傳後文件損壞
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
請幫助我解決任何問題或建議。 謝謝。
你可能不應該在代碼中包含真正的憑據。 – 2015-02-11 18:52:50
是否http://stackoverflow.com/questions/12650013/c-sharp-file-is-corrupt-after-uploaded-to-server幫助? – KenD 2015-02-11 18:54:12
謝謝@ReticulatedSpline。 – ConductedClever 2015-02-11 18:55:45