开发者

How can I Upload File (large size) using FileStream in WebService in IIS(Internt Information Services)

I am using C# to create a service for file uploading. I created three methods:

  • upload_start(string filename)
  • upload_开发者_JAVA技巧continue(string filename, byte[] buffer)
  • upload_end(string filename)
  • It works, but I don't want to handle 3 functions from the client program. How can I open a FileStream from the client program, and let the server side finish the file upload?


    The simplest approach would probably be to create a REST service, either with wcf or do something similar with asp.net, but you would then just do a POST operation, and the server could do the work.

    Here is one approach:

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

    This would have a simpler interface for you.


    I don't think you can as the FileStream is not Serializable. Why not pass the service the file name (as you already are) and have the service open the file and process it.


    For upload a large file in WebServices(not Windows Cominication Foundation WCF) this is the way to follow:

    In the server side file Web.Config add this xml and change the value of maxRequestLength depending on the maximum size of upload, by defoult maxRequestLength is 4MB, in this example 8192=MB

        <httpRuntime
            executionTimeout="600"
            maxRequestLength="8192"
            useFullyQualifiedRedirectUrl="false"
            minFreeThreads="8"
            minLocalRequestFreeThreads="4"
            appRequestQueueLimit="100"
            enableVersionHeader="true"
      />
    

    Add another public function in server side, this function received a byte[] contains the file, the file name and a path where you wont save the file in the server.

     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;
            }
    

    In the client side add this code to send the file:

    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

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜