I have created one WCF service that will upload the file. and after using that service I am trying to upload the file I am able to successfully upload the file but there is some issue with the FILESTREAM class.

The moment i clicked the button to upload the file when i checked by debugging the application i get to know that stream object is null. I am passing the object of stream class to the WCF method. But due to some issue that stream object is getting null. due to that null object of stream class, image which is uploded getting empty in my folder

This is my code that I am using to upload the file

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

Code for WCF Service

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

Related posts

Recent Viewed