在ASP.NET中使用WCF上载文件

时间:2014-08-18 16:20:36

标签: c# asp.net wcf filestream

我创建了一个将上传文件的WCF服务。在使用该服务后,我尝试上传文件,我能够成功上传文件,但FILESTREAM类存在一些问题。

当我通过调试应用程序检查时,单击按钮上传文件的那一刻我知道流对象为空。 我将流类的对象传递给WCF方法。 但由于某些问题,流对象变为空。 由于流类的null对象,在我的文件夹中上传的图像

这是我用来上传文件的代码

 if (FileUpload1.HasFile)
        {
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
            FileTransferServiceReference.ITransferService clientUpload = new FileTransferServiceReference.TransferServiceClient("BasicHttpBinding_ITransferService");
            FileTransferServiceReference.RemoteFileInfo uploadRequestInfo = new RemoteFileInfo();


            string Path = System.IO.Path.GetDirectoryName(FileUpload1.FileName);

            using (System.IO.FileStream stream = new System.IO.FileStream(FileUpload1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                uploadRequestInfo.FileName = FileUpload1.FileName;
                uploadRequestInfo.Length = fileInfo.Length;
                uploadRequestInfo.FileByteStream = stream;
                clientUpload.UploadFile(uploadRequestInfo);
            }
        }

WCF服务代码

public RemoteFileInfo DownloadFile(DownloadRequest request)
        {
            RemoteFileInfo result = new RemoteFileInfo();
            try
            {
                // get some info about the input file
                string filePath = System.IO.Path.Combine(@"c:\Uploadfiles", request.FileName);
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

                // check if exists
                if (!fileInfo.Exists) throw new System.IO.FileNotFoundException("File not found", request.FileName);

                // open stream
                System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

                // return result

                result.FileName = request.FileName;
                result.Length = fileInfo.Length;
                result.FileByteStream = stream;
            }
            catch (Exception ex)
            {

            }
            return result;

        }


        public void UploadFile(RemoteFileInfo request)
        {
            FileStream targetStream = null;
            Stream sourceStream = request.FileByteStream;


            string uploadFolder = @"C:\upload\";
            if (!Directory.Exists(uploadFolder))
            {
                Directory.CreateDirectory(uploadFolder);
            }

            string filePath = Path.Combine(uploadFolder, request.FileName);

            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                const int bufferLen = 65000;
                byte[] buffer = new byte[bufferLen];
                int count = 0;
                while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                }
                targetStream.Close();
                sourceStream.Close();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

发现差异:

 string uploadFolder = @"C:\upload\";

 ...

 string filePath = System.IO.Path.Combine(@"c:\Uploadfiles", request.FileName);

作为一般提示,您可以将上传文件路径放入外部配置文件中,以便在将应用程序移动到需要将数据存储在其他驱动器或特定位置的服务器上时进行更改。 。

另外,您总是调用相同的配置条目,因此您的上传路径名称无处不在。