简体   繁体   English

音频文件无法通过FTP上传以编程方式工作

[英]Audio file is not working via FTP upload programatically

I am uploading an .mp3 file via FTP code using C#, the file is uploaded successfully on server but when i bind to a simple audio control or directly view in browser it does not work as expected, whereas when i upload manually on the server it works perfectly. 我正在使用C#通过FTP代码上传.mp3文件,该文件已成功上传到服务器上,但是当我绑定到简单的音频控件或直接在浏览器中查看时,它无法按预期工作,而当我在服务器上手动上传时完美地工作。

Code: 码:

        var inputStream = FileUpload1.PostedFile.InputStream;
        byte[] fileBytes = new byte[inputStream.Length];
        inputStream.Read(fileBytes, 0, fileBytes.Length);

Note: When i view the file in Firefox it shows MIME type is not supported. 注意:当我在Firefox中查看文件时,表明不支持MIME类型。

Thanks! 谢谢!

You're reading the file as a string then using UTF8 encoding to turn it into bytes. 您正在以字符串形式读取文件,然后使用UTF8编码将其转换为字节。 If you do that, and the file contains any binary sequence that doesn't code to a valid UTF8 value, parts of the data stream will simply get discarded. 如果这样做,并且文件中包含任何未编码为有效UTF8值的二进制序列,则部分数据流将被丢弃。

Instead, read it directly as bytes. 而是直接将其读取为字节。 Don't bother with the StreamReader. 不要理会StreamReader。 Call the Read() method on the underlying stream. 在基础流上调用Read()方法 Example: 例:

var inputStream = FileUpload1.PostedFile.InputStream
byte[] fileBytes = new byte[inputStream.Length];
inputStream.Read(fileBytes, 0, fileStream.Length);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM