【问题标题】:uploading a file using WCF in ASP.NET在 ASP.NET 中使用 WCF 上传文件
【发布时间】:2014-08-18 16:20:35
【问题描述】:

我创建了一个将上传文件的 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();
            }
        }
    }

【问题讨论】:

  • 发送一个字节[]不是更容易吗?
  • 我有点困惑如何实现这一点。你能告诉我怎么可能吗
  • 第一个参数是 filstream 中的完整路径,但我只传递了填充的名称,这会是问题吗?

标签: c# asp.net wcf filestream


【解决方案1】:

找出不同之处:

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

 ...

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

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

同样,您总是调用相同的配置条目,因此您的上传路径名称肯定会在任何地方都相同。

【讨论】:

  • uploadfiles 用于下载的图像。我的主要问题是如果(FileUpload1.HasFile)...此代码,特别是此“流”对象返回空值,请检查是否使用(System.IO.FileStream stream = new System.IO.FileStream(FileUpload1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 有任何错误
  • 第一个参数是 filstream 中的完整路径,但我只传递了填充的名称,这将是问题
  • 调试代码时,文件路径的值是多少,该路径下是否存在文件?
  • 路径返回空字符串,这就是为什么我不使用该路径的值,但文件流的第一个参数是文件的路径,所以我怎样才能获得图像的完整路径我想我我正在通过 E 驱动器上传我的图像,那么有什么方法可以让我在第一个参数中传递完整路径
  • 文件路径是否存在?文件上传到c:\upload\filename 是否如您所愿?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
相关资源
最近更新 更多