c# - 文件上传控件无法返回正确的内容..附加字符

标签 c# asp.net

我有一个文件上传控件。我必须读取一个文件内容。

实际文件内容为

CREATE PROCEDURE dbo.ws_Device_Update
(
    @ApplicationId uniqueidentifier   ,
    @OriginalApplicationId uniqueidentifier   ,
    @DeviceIMEI nvarchar (50)  ,
    @OriginalDeviceIMEI nvarchar (50)  ,
    @ModelId int   ,
    @DeviceName nvarchar (50)  ,
    @DeviceDescription nvarchar (255)  ,
    @DeviceState int   ,
    @IsExpired bit   ,
    @IsSuspended bit   ,
    @LastAccessed datetime   ,
    @ClientSeqNo bigint   ,
    @ServerSeqNo bigint   ,
    @ModelCode varchar (50)  ,
    @PushToken varchar (512)  ,
    @PushLastAlive datetime   ,
    @PushLastDead datetime   ,
    @DeviceType int   
)
AS

我正在使用下面的程序

int fileLength = fileUpload.PostedFile.ContentLength;            
Byte[] contentInBytes = new Byte[fileLength];
using (Stream streamContent = fileUpload.FileContent)
{
  streamContent.Read(contentInBytes, 0, fileLength);
}
var xx = ASCIIEncoding.ASCII.GetString(contentInBytes);

我得到的输出是

???CREATE PROCEDURE dbo.ws_Device_Update
(
    @ApplicationId uniqueidentifier   ,
    @OriginalApplicationId uniqueidentifier   ,
    @DeviceIMEI nvarchar (50)  ,
    @OriginalDeviceIMEI nvarchar (50)  ,
    @ModelId int   ,
    @DeviceName nvarchar (50)  ,
    @DeviceDescription nvarchar (255)  ,
    @DeviceState int   ,
    @IsExpired bit   ,
    @IsSuspended bit   ,
    @LastAccessed datetime   ,
    @ClientSeqNo bigint   ,
    @ServerSeqNo bigint   ,
    @ModelCode varchar (50)  ,
    @PushToken varchar (512)  ,
    @PushLastAlive datetime   ,
    @PushLastDead datetime   ,
    @DeviceType int   
)
AS

一些额外的 ??? 字符即将到来......我不知道从哪里来?

产生的字节数组是

239,187,191,67,82,69,65,84,69,32,80,82,79,67,69,68,85,82,69,32,100,98,111,46,119,115,95,68,101,118,105,99,101,95, 85,112,100,97,116,101,13,10,40,13,10,9,64,65,112,112,108,105,99,97,116,105,111,110,73,100,32,117,110,105,113,117,               101,105,100,101,110,116,105,102,105,101,114,32,32,32,44,13,10,9,64,79,114,105,103,105,110,97,108,65,112,112,108,                105,99,97,116,105,111,110,73,100,32,117,110,105,113,117,101,105,100,101,110,116,105,102,105,101,114,32,32,32,44,                13,10,9,64,68,101,118,105,99,101,73,77,69,73,32,110,118,97,114,99,104,97,114,32,40,53,48,41,32,32,44,13,10,9,64,                79,114,105,103,105,110,97,108,68,101,118,105,99,101,73,77,69,73,32,110,118,97,114,99,104,97,114,32,40,53,48,41,32,                32,44,13,10,9,64,77,111,100,101,108,73,100,32,105,110,116,32,32,32,44,13,10,9,64,68,101,118,105,99,101,78,97,109,                101,32,110,118,97,114,99,104,97,114,32,40,53,48,41,32,32,44,13,10,9,64,68,101,118,105,99,101,68,101,115,99,114,105,                112,116,105,111,110,32,110,118,97,114,99,104,97,114,32,40,50,53,53,41,32,32,44,13,10,9,64,68,101,118,105,99,101,83,                116,97,116,101,32,105,110,116,32,32,32,44,13,10,9,64,73,115,69,120,112,105,114,101,100,32,98,105,116,32,32,32,44,13,                10,9,64,73,115,83,117,115,112,101,110,100,101,100,32,98,105,116,32,32,32,44,13,10,9,64,76,97,115,116,65,99,99,101,115,                115,101,100,32,100,97,116,101,116,105,109,101,32,32,32,44,13,10,9,64,67,108,105,101,110,116,83,101,113,78,111,32,98,105,                103,105,110,116,32,32,32,44,13,10,9,64,83,101,114,118,101,114,83,101,113,78,111,32,98,105,103,105,110,116,32,32,32,44,13,                10,9,64,77,111,100,101,108,67,111,100,101,32,118,97,114,99,104,97,114,32,40,53,48,41,32,32,44,13,10,9,64,80,117,115,104,84,                111,107,101,110,32,118,97,114,99,104,97,114,32,40,53,49,50,41,32,32,44,13,10,9,64,80,117,115,104,76,97,115,116,65,108,105,                118,101,32,100,97,116,101,116,105,109,101,32,32,32,44,13,10,9,64,80,117,115,104,76,97,115,116,68,101,97,100,32,100,97,116,                101,116,105,109,101,32,32,32,44,13,10,9,64,68,101,118,105,99,101,84,121,112,101,32,105,110,116,32,32,32,13,10,41,13,10,65,83

我不知道“239,187,191”是从哪里来的...关于如何解决这个问题的任何线索?

谢谢

最佳答案

239 187 191 代表UTF-8 Byte order mark

尝试 StreamReader Constructor (String, Encoding, Boolean)它有检测编码的参数

StreamReader reader = new StreamReader(fileUpload.FileContent, 
                  Encoding.Unicode, true);

string fromFile= reader.ReadToEnd();

关于c# - 文件上传控件无法返回正确的内容..附加字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16161221/

相关文章:

c# - ASP.MVC 的显示值转换器

.net - 更改 "project.net"的默认主题

c# - 异步方法在 C# 中如何工作?

c# - 如何使用 .NET 安装和设置数据库?

c# - 什么是托管代码和非托管代码?

c# - 如何触发通用类方法以递归方式更改 T 的类型?

c# - 从数据库中删除模型数据时,除主键之外的所有内容都会被删除,我该如何删除所有数据?

c# - 在 WCF 服务契约(Contract)中使用 soapAction =""进行多项操作?

c# - 具有不同长度但相同单词的字符串相似性

c# - 重新启动具有不同输入的线程