首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在IIS()中使用FileStream在WebService中上传文件(大容量)

如何在IIS()中使用FileStream在WebService中上传文件(大容量)
EN

Stack Overflow用户
提问于 2011-05-21 03:39:05
回答 3查看 11.3K关注 0票数 2

我正在使用C#创建一个文件上传服务。我创建了三个方法:

  • upload_start(string filename)
  • upload_continue(string filename, byte[] buffer)
  • upload_end(string filename)

它可以工作,但我不想处理客户端程序中的3个函数。如何从客户端程序打开FileStream,让服务器端完成文件上传?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-21 18:46:22

最简单的方法可能是使用wcf或使用asp.net创建REST服务,但是您只需执行POST操作,服务器就可以完成这项工作。

以下是一种方法:

http://debugmode.net/2011/05/01/uploading-file-to-server-from-asp-net-client-using-wcf-rest-service/

这将为您提供一个更简单的界面。

票数 0
EN

Stack Overflow用户

发布于 2011-05-21 03:49:57

我不认为你可以,因为FileStream是不可序列化的。为什么不将文件名传递给服务(就像您已经知道的那样),让服务打开文件并处理它。

票数 0
EN

Stack Overflow用户

发布于 2014-10-18 08:06:25

要在WebServices(不是)中上传一个大文件,请遵循以下方法:

在服务器端文件Web.Config中,添加此xml并根据上传的最大大小更改maxRequestLength的值,在本例中,maxRequestLength为4MB。

代码语言:javascript
运行
复制
    <httpRuntime
        executionTimeout="600"
        maxRequestLength="8192"
        useFullyQualifiedRedirectUrl="false"
        minFreeThreads="8"
        minLocalRequestFreeThreads="4"
        appRequestQueueLimit="100"
        enableVersionHeader="true"
  />

在服务器端添加另一个公共函数,此函数接收到的byte[]包含文件、文件名和路径,您不会将文件保存在服务器中。

代码语言:javascript
运行
复制
 public String UploadFile(byte[] fileByte, String fileName, String savePath)
        {

            string newPath = savePath;


            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }

            newPath = newPath + fileName;

            System.IO.FileStream fs1 = null;
            byte[] b1 = null;
            b1 = fileByte;
            fs1 = new FileStream(newPath, FileMode.Create);
            fs1.Write(b1, 0, b1.Length);
            fs1.Flush();
            fs1.Close();
            fs1 = null;

            return newPath;
        }

在客户端,添加以下代码以发送文件:

代码语言:javascript
运行
复制
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Select a File"; 
            dlg.DefaultExt = ".xls";  
            dlg.Filter = "Excel documents (.xls)|*.xls";  

            // Show open file dialog box 
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results 
            if (result == true)
            {

                string filename = dlg.FileName;


                string file = Path.GetFileName(filename);

                System.IO.FileStream fs1 = null;
                fs1 = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read);
                byte[] b1 = new byte[fs1.Length];
                fs1.Read(b1, 0, (int)fs1.Length);
                fs1.Close();


                String savePath = @"C:\DOC\IMPORT\";
                String newPath = WEB_SERVICES.UploadFile(b1, file, savePath);


                MessageBox.Show(String.Format("The file is uploaded in {0}", newPath));


            }
            else
            {
                MessageBox.Show("Select a file, please");
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6079445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档