Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过web API检查接收到的文件字节并保存在服务器上?_C#_Jquery_Asp.net Web Api_Web - Fatal编程技术网

C# 如何通过web API检查接收到的文件字节并保存在服务器上?

C# 如何通过web API检查接收到的文件字节并保存在服务器上?,c#,jquery,asp.net-web-api,web,C#,Jquery,Asp.net Web Api,Web,我必须使用Web API编写服务。在该方法中,我应收到2项: 一个Id 文件 将从移动应用程序调用此方法。在谷歌搜索了这个主题之后,在一个示例网页中,我将上传的文件转换为base 64(使用jQuery),并在服务器上接收到它: <script type="text/javascript"> // Holds the load event results var base64 = ""; // function converToBase64() {

我必须使用Web API编写服务。在该方法中,我应收到2项:

  • 一个Id
  • 文件
  • 将从移动应用程序调用此方法。在谷歌搜索了这个主题之后,在一个示例网页中,我将上传的文件转换为base 64(使用jQuery),并在服务器上接收到它:

    <script type="text/javascript">
        // Holds the load event results
        var base64 = "";
    
        //
        function converToBase64() {
            var file = document.querySelector('input[type=file]').files[0];
    
            var reader = new FileReader();
    
            // Event handler
            reader.addEventListener("load", function () {
                base64 = reader.result;
                console.log(base64);
    
            }, false);
    
            // Fires the load event
            if (file) {
                reader.readAsDataURL(file);
    
            }
        }
    
        // AJAX request
        $(function () {
            $('#Button1').click(function () {
    
                var data = {};
    
                data.Id = "3";
                data.File = base64;
                console.log(data);
                alert("start");
                $.ajax({
                    type: "POST",
                    url: "/api/SaveFile",
                    data: JSON.stringify(data),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                }).done(function (result) {
                    console.log(result);
                    });
                })
            });
    
    
    </script>
    
    <div>
        <input type="file" onchange="converToBase64()"><br>
        <input id="Button1" type="button" value="Submit" />
    </div>
    

    我读过很多关于将字节保存到文件中的相同问题的解决方案。但在检查扩展和大小之前,我不应该这样做。由于在物理空间中保存被禁止的文件是危险的,所以在保存到磁盘(作为文件)之前,如何进行这些检查?因为。

    据我所知,您的Web API方法中没有可用的文件名或扩展名。你想做什么样的检查?文件的类型决定了您可能暴露于哪些漏洞。你的问题太开放了,我们需要更多的信息来正确回答。@Snympi,我应该让图像文件保存在磁盘上。不是其他文件扩展名。因此您正在上载图像文件。您已经缩小了问题的范围,但是您仍然必须能够知道上载的图像格式(通过使用文件扩展名,例如.jpg、.png、.bmp等)。在过去,某些图像文件类型有特定的漏洞,但从那时起就得到了解决。还有,你想做什么检查?我要接受.jpg、.png、.bmp,你想做什么检查?
    [HttpPost]
    public HttpResponseMessage Post(FileUploadData data)
    {
        string converted = data.File.Substring(data.File.IndexOf(",") + 1, data.File.Length - data.File.IndexOf(",") - 1);
        byte[] fileBytes = Convert.FromBase64String(converted);
    
        // Do checking
        return Request.CreateResponse(HttpStatusCode.OK, "Success");
    }
    
    public class FileUploadData
    {
        public string Id { get; set; }
        public string File { get; set; }
    }