Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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
Javascript 在ASP中仅使用jquery和Web服务从客户端上载图像_Javascript_Jquery_Asp.net_Web Services_Wcf - Fatal编程技术网

Javascript 在ASP中仅使用jquery和Web服务从客户端上载图像

Javascript 在ASP中仅使用jquery和Web服务从客户端上载图像,javascript,jquery,asp.net,web-services,wcf,Javascript,Jquery,Asp.net,Web Services,Wcf,在我的应用程序中,我想从客户端上传一些图像。我想在没有任何处理程序的情况下这样做。我希望使用jquery从客户端(本地驱动器)浏览映像,并希望将该映像传递给一个web服务(从jquery本身),并仅使用该服务保存该映像,而不使用任何其他处理程序 public string UploadImageUser(string foto) { if (foto != "") { var regex = new Regex(@ "data:(?<mime>[\w/\-\.

在我的应用程序中,我想从客户端上传一些图像。我想在没有任何处理程序的情况下这样做。我希望使用jquery从客户端(本地驱动器)浏览映像,并希望将该映像传递给一个web服务(从jquery本身),并仅使用该服务保存该映像,而不使用任何其他处理程序

public string UploadImageUser(string foto) {
    if (foto != "") {
        var regex = new Regex(@ "data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);
        var match = regex.Match(foto);

        var mime = match.Groups["mime"].Value;
        var encoding = match.Groups["encoding"].Value;
        var data = match.Groups["data"].Value;
        try {
            string path = "C://";
            Byte[] bytes = Convert.FromBase64String(data);
            File.WriteAllBytes(path + "example.jpg", bytes);
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}
我尝试过类似的不同帖子,但没有一个能满足我的需求

public string UploadImageUser(string foto) {
    if (foto != "") {
        var regex = new Regex(@ "data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);
        var match = regex.Match(foto);

        var mime = match.Groups["mime"].Value;
        var encoding = match.Groups["encoding"].Value;
        var data = match.Groups["data"].Value;
        try {
            string path = "C://";
            Byte[] bytes = Convert.FromBase64String(data);
            File.WriteAllBytes(path + "example.jpg", bytes);
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}
我不熟悉.net和jquery,所以无论是否可能,我尝试获取上传图像的路径,但后来我明白了,因为安全浏览器不会给出客户端的实际路径

public string UploadImageUser(string foto) {
    if (foto != "") {
        var regex = new Regex(@ "data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);
        var match = regex.Match(foto);

        var mime = match.Groups["mime"].Value;
        var encoding = match.Groups["encoding"].Value;
        var data = match.Groups["data"].Value;
        try {
            string path = "C://";
            Byte[] bytes = Convert.FromBase64String(data);
            File.WriteAllBytes(path + "example.jpg", bytes);
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}

那么,是否还有其他方法可以做到这一点

试试这段代码。这段代码可以帮助你吗
public string UploadImageUser(string foto) {
    if (foto != "") {
        var regex = new Regex(@ "data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);
        var match = regex.Match(foto);

        var mime = match.Groups["mime"].Value;
        var encoding = match.Groups["encoding"].Value;
        var data = match.Groups["data"].Value;
        try {
            string path = "C://";
            Byte[] bytes = Convert.FromBase64String(data);
            File.WriteAllBytes(path + "example.jpg", bytes);
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}
为Web服务创建ajax POST请求

<script type="text/javascript">
$(document).ready(function () {
   $('#btnUploadFile').on('click', function () {
      var data = new FormData();
      var files = $("#fileUpload").get(0).files;
      // Add the uploaded image content to the form data collection
      if (files.length > 0) {
           data.append("UploadedImage", files[0]);
      }

      // Make Ajax request with the contentType = false, and procesDate = false
      var ajaxRequest = $.ajax({
           type: "POST",
           url: "/api/fileupload/uploadfile",
           contentType: false,
           processData: false,
           data: data
           });

      ajaxRequest.done(function (xhr, textStatus) {
                    // Do other operation
             });
   });
});
</script>
public string UploadImageUser(string foto) {
    if (foto != "") {
        var regex = new Regex(@ "data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);
        var match = regex.Match(foto);

        var mime = match.Groups["mime"].Value;
        var encoding = match.Groups["encoding"].Value;
        var data = match.Groups["data"].Value;
        try {
            string path = "C://";
            Byte[] bytes = Convert.FromBase64String(data);
            File.WriteAllBytes(path + "example.jpg", bytes);
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}

我处理过从AngularJS上升到asp的图像,对我来说,最好的方法是使用代码base64和Web服务转换图像并将其保存到特定路径

public string UploadImageUser(string foto) {
    if (foto != "") {
        var regex = new Regex(@ "data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);
        var match = regex.Match(foto);

        var mime = match.Groups["mime"].Value;
        var encoding = match.Groups["encoding"].Value;
        var data = match.Groups["data"].Value;
        try {
            string path = "C://";
            Byte[] bytes = Convert.FromBase64String(data);
            File.WriteAllBytes(path + "example.jpg", bytes);
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}
我尝试使用base64,并在此处添加代码

public string UploadImageUser(string foto) {
    if (foto != "") {
        var regex = new Regex(@ "data:(?<mime>[\w/\-\.]+);(?<encoding>\w+),(?<data>.*)", RegexOptions.Compiled);
        var match = regex.Match(foto);

        var mime = match.Groups["mime"].Value;
        var encoding = match.Groups["encoding"].Value;
        var data = match.Groups["data"].Value;
        try {
            string path = "C://";
            Byte[] bytes = Convert.FromBase64String(data);
            File.WriteAllBytes(path + "example.jpg", bytes);
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
}
   var filesSelected = document.getElementById("inputFileToLoad").files;
    if (filesSelected.length > 0) {
        var fileToLoad = filesSelected[0];
        var fileReader = new FileReader();
        fileReader.onload = function (fileLoadedEvent) {
            var srcData = fileLoadedEvent.target.result; // <--- data: base64
            var base64 = srcData;
            //jpeg or png
            base64 = base64.replace('data:image/jpeg;base64,', '');
            $.ajax({
                type: "POST",
                //url: AQ.AMT.Url + "/Home/StoreImage",
                url: "DempService.asmx/StoreImage",
                //async: false,
                data: "{'base64':'" + base64 + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (Data) {
                    alert('ok');
                 },
                error: function (e) {

                }
            });
        }
        fileReader.readAsDataURL(fileToLoad);
    }

首先,我将把图像转换成base64,然后我将把它传递给web服务,我知道如何将图像转换成base64,但我不知道如何将它传递给服务,你能调用ajax吗..当你在使用asp时?例如,您是否有代码作为接收数据@User3501613编辑我的帖子@User3501613我在web服务中使用了不同的功能。你能告诉我哪一个更好吗?我正在用你的answer@user3501613在这里我们可以发现不同,在图像格式中有不同类型的标题。但在本例中,当我们使用base64代码处理图像时,我认为这两种解决方案都适用。你能说出上面的方法和这个方法之间的区别吗…@user3501613这两个代码中唯一的区别是请求头和图像格式。上面的代码将发布带有base64转换的图像数据,您将获得方法param,在这个代码文件中,数据将作为请求paramSo出现在上面的方法2转换所需的image-base64中,反之亦然。所以我认为这将减轻这个负担。。。