简体   繁体   English

如何在C#中的WCF REST API上使用Stream将文件(图像/视频/等)正确上传到服务器?

[英]How to upload files(images/videos/etc) to server using Stream on WCF REST API in C# properly?

I'm setting up a function to upload images/videos on server using WCF REST API. 我正在设置使用WCF REST API在服务器上上传图像/视频的功能。 The files are successfully uploaded in the proper destination folder, but they end up becoming unreadable no matter what kind of file it is. 这些文件已成功上传到正确的目标文件夹中,但是无论文件是哪种类型,它们最终都变得不可读。

Is there something wrong within my code (especially on the FileStream-Write part) that cause that to happen? 我的代码中是否存在某些错误(尤其是在FileStream-Write部分)导致该错误发生? Or is it possible that the problem lies elsewhere (such as the Web.config file)? 还是问题可能出在其他地方(例如Web.config文件)?

Here's my code snippet: 这是我的代码段:

public string uploadFile(Stream fileStream)
        {
            String fileName = System.Web.HttpContext.Current.Request.QueryString["fileName"];
            String destFileName = HHCWCFApp.Properties.Settings.Default.TemporaryFilePath + fileName;
            String destLink = HHCWCFApp.Properties.Settings.Default.Hyperlink + fileName;

            try
            {
                int length = 256;
                int bytesRead = 0;
                Byte[] buffer = new Byte[length];
                using (FileStream fs = new FileStream(destFileName, FileMode.Create))
                {
                    do
                    {
                        bytesRead = fileStream.Read(buffer, 0, length);
                        fs.Write(buffer, 0, bytesRead);
                    }
                    while (bytesRead == length);
                }

                fileStream.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            if (File.Exists(destFileName))
            {
                return destLink;
            }
            else
            {
                return "Not Found";
            }
        }

What kinds of files are you uploading? 您要上传哪种文件? larger files may fall foul of the maximum request length, this is set in the web config as below 较大的文件可能违反最大请求长度,这是在Web配置中设置的,如下所示

 <configuration> 
<system.web> 
    <httpRuntime maxRequestLength="xxxx" /> 
</system.web> 

Try a text file with only a few kb and if that works but the larger files don't then this could very well solve the issue. 尝试使用只有几个kb的文本文件,如果可以,但是较大的文件不起作用,则可以很好地解决此问题。 I've tested your code and there isn't a problem there. 我已经测试了您的代码,那里没有问题。

also bear in mind that the IIS server you use may also have the maximum request length set which may override your value. 还请记住,您使用的IIS服务器可能还设置了最大请求长度,该长度可能会覆盖您的值。

The default length is 4mb and you could write a function to retrieve the value so your client/calling code could do a check to see if the file it's going to pass exceeds the max size. 默认长度为4mb,您可以编写一个函数来检索该值,以便您的客户端/调用代码可以检查要传递的文件是否超过最大大小。

have a read here on Microsoft's page for a bit more info. 请在Microsoft页面上阅读此处以获取更多信息。

Edit: misread the code first time round, apologies 编辑:第一次误读代码,抱歉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM