日期:2014-05-17  浏览次数:20808 次

C# 支持docx上传
使用下面这个函数上传的时候 可以上传成功 但是docx格式有损坏 如何改善


    protected bool UpLoadFileStream(HttpPostedFile postfile, string saveFilePath)
    {
        System.IO.Stream iStream = null;
        // Buffer to read 10K bytes in chunk:
        byte[] buffer = new Byte[10000];
        int length = 0;
        // Total bytes to read:
        long dataToRead;
        Stream GetStream = postfile.InputStream;
        string fileName = System.IO.Path.GetFileName(postfile.FileName);
        //string filepath = Server.MapPath(saveFilePath + "\\" + fileName);
        if (File.Exists(saveFilePath) == false)
        {
            FileStream FS = new FileStream(saveFilePath, FileMode.CreateNew);
            BinaryWriter BW = new BinaryWriter(FS);
            try
            {
                dataToRead = GetStream.Length;//iStream.Length;
                while (dataToRead > 0)
                {
                    // Verify that the client is connected.
                    if (Response.IsClientConnected)
                    {
                        // Read the data in buffer.
                        length = GetStream.Read(buffer, 0, 10000);

                        // Write the data to the current output stream.
                        BW.Write(buffer, 0, 10000);
                        //buffer = new Byte[10000];
                        dataToRead = dataToRead&nb