使用c#中的jquery ajax将文件保存在文件夹中?

时间:2017-04-20 05:25:01

标签: javascript c# jquery ajax

我能够在.cs页面和图像上发送数据也保存在文件夹中但它已损坏且图像大小为0 ..我完全感到困惑,错误在哪里 请参阅 JQUERY CODE& C#CODE

JQUERY代码

<script type="text/javascript">
    $(document).ready(function () {
        $("#submitPhoto").click(function () {
            var photo = $("#uploadPhoto").get(0);
            var file = photo.files;
            var Photo_Path = $("#uploadPhoto").val();
            var extenssion = Photo_Path.split('.').pop().toUpperCase();
            if (Photo_Path == "") {
                alert('Please Choose Photo');
                return;
            }

            if (extenssion != 'JPEG' && extenssion != 'JPG' && extenssion != 'jpg') {
                alert('Invalid File[JPEG,JPG is acceptable]');
                return;
            }
            if (file.size < 4000 || file.size > 14000) {
                alert('Photo Size Must be Below 12Kb and Above 4Kb ');
                return;
            }
            var id = $("#submitPhoto").attr('id');                

            $.ajax({
                type: "POST",
                contentType: "multipart/form-data",
                url: "finalstep-registration.aspx?photoPath=" + Photo_Path + "&id=" + id + "&check=imgUpload",
                data: file,
                datatype: 'json',
                async: false,
                processData: false,
                success: function (data) {

                    alert('Image successfully Uploaded');

                },
                error: function ()
                { console.log('there is some error'); }
            });
        });
 </script>

C#代码 将图像保存在文件夹中,名称保存在数据库中 我使用查询字符串

在pageload上调用uploadPhoto()函数
public void uploadPhoto(string photoPath,string id)
{
    //HttpContext context = HttpContext.Current;        
    //int regNo = Convert.ToInt32(Session["restrationNumber"].ToString());
    int regNo = 1001;

    if (id == "submitPhoto")
    {
        FileInfo CustPic = new FileInfo(photoPath);
        System.IO.Stream fileContent = Request.InputStream;
        FileStream fileStream = File.Create(Server.MapPath("~/photo/") + CustPic.Name);
        fileContent.Seek(0, System.IO.SeekOrigin.Begin);
        fileContent.CopyTo(fileStream);
        //System.Web.Hosting.HostingEnvironment.MapPath("~/photo/");            
        string query = "update userDetails set photo='"+photoPath+ "' where regNo="+regNo+"";
        int a = bl.executenonquery(query);
        if (a == 1)
            Response.Write("Photo is uploaded successfully");
        else
            Response.Write("Oops, We can't upload your photo. try again");

        //Session["photo"] = CustPic.Name;

    }
}

检查并告诉我我在哪里做错了

0 个答案:

没有答案