2012-02-17 40 views
0

我正在嘗試使用谷歌的語音API進行語音識別。我的代碼一直使用文件,無法手動刪除或使用代碼

我修改,可以在CodeProject上找到UPLOADFILEEX功能...

文件我想刪除是C:\ record.flac

這裏是低於

  Public Shared Function UploadFile(ByVal uploadfilename As String, ByVal url As String, ByVal fileFormName As String, ByVal contenttype As String, ByVal querystring As System.Collections.Specialized.NameValueCollection, ByVal cookies As CookieContainer) As String 
    If (fileFormName Is Nothing) OrElse (fileFormName.Length = 0) Then 
     fileFormName = "file" 
    End If 
    If (contenttype Is Nothing) OrElse (contenttype.Length = 0) Then 
     contenttype = "application/octet-stream" 
    End If 
    Dim postdata As String 
    postdata = "?" 
    If Not (querystring Is Nothing) Then 
     For Each key As String In querystring.Keys 
      postdata += key + "=" + querystring.Get(key) + "&" 
     Next 
    End If 
    Dim uri As Uri = New Uri(url + postdata) 
    Dim boundary As String = "----------" + DateTime.Now.Ticks.ToString("x") 
    Dim webrequest As HttpWebRequest = CType(Net.WebRequest.Create(uri), HttpWebRequest) 
    webrequest.CookieContainer = cookies 
    webrequest.ContentType = "audio/x-flac; rate=16000" 
    webrequest.Method = "POST" 
    Dim sb As StringBuilder = New StringBuilder 
    sb.Append("--") 
    sb.Append(boundary) 
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "") 
    sb.Append("Content-Disposition: form-data; name=""") 
    sb.Append(fileFormName) 
    sb.Append("""; filename=""") 
    sb.Append(IO.Path.GetFileName(uploadfilename)) 
    sb.Append("""") 
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "") 
    sb.Append("Content-Type: ") 
    sb.Append(contenttype) 
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "") 
    sb.Append("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "") 
    Dim postHeader As String = sb.ToString 
    Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(postHeader) 
    Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes("" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "--" + boundary + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "") 
    Dim fileStreama As FileStream = New FileStream(uploadfilename, FileMode.Open, FileAccess.Read) 
    Dim length As Long = postHeaderBytes.Length + fileStreama.Length + boundaryBytes.Length 
    webrequest.ContentLength = length 
    Dim requestStream As Stream = webrequest.GetRequestStream 
    requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length) 
    Dim sendBuffer(Math.Min(4096, fileStreama.Length)) As Byte 
    Dim bytesRead As Integer = 0 
    Do 
     bytesRead = fileStreama.Read(sendBuffer, 0, sendBuffer.Length) 
     If bytesRead = 0 Then Exit Do 
     requestStream.Write(sendBuffer, 0, bytesRead) 
    Loop 
    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length) 
    Dim responce As WebResponse = webrequest.GetResponse 
    Dim s As Stream = responce.GetResponseStream 
    Dim sr As StreamReader = New StreamReader(s) 
    Return sr.ReadToEnd 
    sr.Dispose() 
    s.Dispose() 
    fileStreama.Dispose() 
    requestStream.Dispose() 
    webrequest.Abort() 
    responce.Close() 

End Function 

功能功能WORKS(Thankgod),但每當我想清除(即刪除位於C :)的音頻文件它只是掛起,沒有任何反應...

下面是我的代碼,對事件的FormClosing執行....

 Private Sub Form1_Close(sender As System.Object, e As System.EventArgs) Handles MyBase.FormClosing 
    Dim di As New IO.DirectoryInfo("c:\") 
    Dim diar1 As IO.FileInfo() = di.GetFiles() 
    Dim dra As IO.FileInfo 
    For Each dra In diar1 
     If dra.Name.Contains("record.") Then 
      dra.Delete() 
     End If 
    Next 
End Sub 

正如你可能也許能夠從我試圖消除所有的流和關閉它們使文件不被功能看訪問

但它仍然掛起,當我嘗試手動刪除它......它告訴我它正在使用另一個進程(這是我的程序) - (我使用ffmpeg將.wav轉換爲.flac文件,但這不會導致任何問題)...

我在做什麼錯...

我是否錯過了一些小溪或什麼的關閉。

由uploadfilename字符串的方式是c:\ record.flac(只爲您的信息 - 我不認爲這會幫助)

回答

0

你的功能是調用Return前的文件流的處置。這意味着您的Dispose調用永遠不會被觸發..並且流保存到文件引用中。

+0

感謝它的工作!我不敢相信我以前沒有想到過! – 2012-02-19 14:55:52

+0

@ImranAhmed - 我很驚訝你沒有得到關於無法訪問的代碼的編譯器警告!選項是否嚴格打開? – 2012-02-20 15:02:53

0

我同意噓,處置前的回報可能是原因。所以解決方法是將sr.ReadToEnd的結果存儲在一個變量中,關閉流,然後返回var。

但是更好的解決方案是在每個必須調用的類上使用Using語句(http://msdn.microsoft.com/en-us/library/htd05whh.aspx).Dispose()on也就是實現IDisposable的類)。

這給出了更清晰的代碼,因爲您可以輕鬆查看代碼中哪些資源正在使用。與.NET相比,更少bug的代碼可確保在您離開使用塊的範圍後,所有內容都會被丟棄。

+0

再次感謝您的評論(我如何回答此問題爲「回答」?) – 2012-02-19 14:56:12

+0

我認爲每個答案旁都應該有一個大的複選標記,您可以單擊它。 – SpoBo 2012-02-20 13:10:49