2012-02-17 35 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