在WINFORMS中将整个文件读入字节数组

发布于 2024-12-11 08:16:31 字数 870 浏览 0 评论 0原文

我想读取使用文件对话框打开的文件的内容,然后将其保存在字节数组中以将其传递给 Web 服务

        Stream myStream;
        OpenFileDialog saveFileDialog1 = new OpenFileDialog();

        saveFileDialog1.Filter = "zip files (*.zip)|*.zip|All files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((myStream = saveFileDialog1.OpenFile()) != null)
            {

                NSITESERVICE.UploadSoapClient obj = new NSITESERVICE.UploadSoapClient();

                byte[] filebytes =  //what should i pass it over here...

                obj.UploadFile("kamal", "p@ssword", filebytes);

                // Code to write the stream goes here.
                myStream.Close();
            }
        }

我不知道我错在哪里,

感谢任何帮助。特纳克斯

I want to read the content of the file opened using file dialog box and then save it in a byte array to pass it to a web service

        Stream myStream;
        OpenFileDialog saveFileDialog1 = new OpenFileDialog();

        saveFileDialog1.Filter = "zip files (*.zip)|*.zip|All files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if ((myStream = saveFileDialog1.OpenFile()) != null)
            {

                NSITESERVICE.UploadSoapClient obj = new NSITESERVICE.UploadSoapClient();

                byte[] filebytes =  //what should i pass it over here...

                obj.UploadFile("kamal", "p@ssword", filebytes);

                // Code to write the stream goes here.
                myStream.Close();
            }
        }

I dont know where i am wrong

Any help is appreciated. Thnaks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妳是的陽光 2024-12-18 08:16:31

您没有为 filebytes 变量分配任何内容,因此实际上是将 null 传递给服务。使用 File.ReadAllBytes 方法读取所有字节并将其传递给网络服务。

You are not assigning anything to filebytes variable so you are essentially passing null to the service. Use File.ReadAllBytes method to read all the bytes and pass it to the webservice.

赠我空喜 2024-12-18 08:16:31

您实际上并没有从 myStream 中读取字节。

byte[] fileBytes = new byte[myStream.Length];
myStream.Read(fileBytes,0,mystream.Length);

obj.UploadFile(...)

You're not actually reading the bytes out of the myStream.

byte[] fileBytes = new byte[myStream.Length];
myStream.Read(fileBytes,0,mystream.Length);

obj.UploadFile(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文