English 中文(简体)
为什么我可以使用 FileSystemObjects 读写客户端的二进制文件, 而不是读取并发送到服务器?
原标题:Why can I use FileSystemObjects for reading and writing client-side binary files, but not for reading and sending them to the server?

我以下列方式创建了二进制文件(以确保二进制文件中包含所有可能的字节值):

using (var fs = File.Create(fileName))
{
    for (byte b = 0; b < Byte.MaxValue; b++)
    {
        fs.WriteByte(b);
    }
}

我这样阅读它(用来试验它的效果):

using (var fs = File.Open(fileName, FileMode.Open))
{
    long oldPos = -1;
    long pos = 0;
    while (oldPos != pos)
    {
        oldPos = pos;
        Console.WriteLine(Convert.ToString(fs.ReadByte(), 2).PadLeft(8,  0 ));
        pos = fs.Position;
    } 
}

使用 < code> FileSystemObject 时, 复制文件( 读取它, 然后把它写回去) 的工作效果很好 :

var fso = new ActiveXObject("Scripting.FileSystemObject");
var from = fso.OpenTextFile(fileToRead, 1, 0); // read, ASCII (-1 for unicode)
var to = fso.CreateTextFile(fileToWriteTo, true, false); 
while (!from.AtEndOfStream) {
    to.Write(from.Read(1));
}
from.Close();
to.Close();

当我读到输出的二进制文件时, 我得到1000,000,000,000,000,1000,000...

但是,试图把它读成 Javascript 似乎造成未能读取:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var from = fso.OpenTextFile(fileToRead, 1, 0); 
var test = [];
while (!from.AtEndOfStream) {
    test.push(0xff & from.Read(1));  // make it a byte.
}
from.Close();

其结果就是测试在阵列内有一组零秒, 以及其他一些非零项, 但大多只有0秒。

有人能解释一下为什么它为一方工作,而不是另一方工作吗?我需要做些什么才能把数值写进 Javascript?

http://www.javascripter.net/faq/reading2.htm" rel="nofollow"。 在阅读客户机上的文件 上,

问题回答

首先, 您知道最后数组的长度是否与文件相同吗?

试一下读和“推”在 seperate operations 中,比如:

...  
Test2 = from.Read(1));  
// Possibly display value of Test2 as string  
test.push(Test2);  
...  

另外,您也可以尝试用文本数据来尝试这个方法,看看它是否是文件/数据的二进制性质引发问题的。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签