无法将图像上传到数据库和应用程序(fileuploader js文件)? fileuploader js

时间:2014-02-27 06:41:56

标签: javascript asp.net-mvc-4 file-upload

我的要求是我必须使用fileuploaderjs文件更新数据库和应用程序中的图像。但我要上传应用程序或数据库。下面是我的代码,请帮助我。我的数据库名称图片有两列,即imagename,image.actually一旦我们读取流数据,其中的内容变为null。所以我无法进一步使用该流..这就是为什么我无法在数据库和应用程序文件夹中上传。我能够在数据库或应用程序文件夹中上传。

.cshtml

<link href="../../Content/fileuploader.css" rel="stylesheet" type="text/css" />
 <script src="../../Scripts/fileuploader.js" type="text/javascript"></script>
 <script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
 <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function () {
    alert('in');
    var uploader = new qq.FileUploader({

        // pass the dom node (ex. $(selector)[0] for jQuery users)
        element: document.getElementById('file-uploader-demo1'),
        action: '/Home/DigitalAssetsFileUpload?fileType=image',

        template: '<div class="qq-uploader">' +
            '<ul  class="qq-upload-list"></ul>' +
            '<a class="qq-upload-button"  >Upload a file</a>' +
              '</div>',
        multiple: false,
        allowedExtensions: ['jpg', 'png', 'gif'],
        debug: true,
        onComplete: function (id, fileName, responseJSON) {

                alert('inserted');

        }
    });

 });



 </script>
 <div style="margin: -5px 0pt 4px 54px; position: relative; position: relative;">
 <div id="file-uploader-demo1" style="text-align: right; width: 0px; padding-left:   
 157px;">
</div>
  </div>







**controller class** 


 this is controller which i have .


   public void DigitalAssetsFileUpload(string recFileType = "image")
    {
        long fileSizeInBytes = 0;
        string fileType = HttpContext.Request.QueryString["fileType"];
         string phyicalFilePath = Server.MapPath("~/Images");
         string uploadedFileName = HttpContext.Request.Headers["X-File-Name"];
        uploadedFileName = HttpUtility.UrlDecode(uploadedFileName);





        string fileExt = Path.GetExtension(uploadedFileName);
        Stream inputstream = null;

        inputstream = HttpContext.Request.InputStream;


        string storagePath = phyicalFilePath;
        if (!Directory.Exists(storagePath))
        {
            System.IO.Directory.CreateDirectory(storagePath);
        }
        string pFilePath = storagePath + "\\" +uploadedFileName;
        FileStream fileStream = new FileStream(pFilePath, FileMode.OpenOrCreate);





        if (inputstream == null)
        {
            System.Diagnostics.Debug.WriteLine("Application", "stream is null");
            throw new NullReferenceException("stream is null");
        }
        fileSizeInBytes = inputstream.Length;

        using (fileStream)
        {
            using (inputstream)
            {
                byte[] buffer = new byte[16 * 1024];
                int bytesRead;

                while ((bytesRead = inputstream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fileStream.Write(buffer, 0, bytesRead);
                }

            }
        }

        BinaryReader br = new BinaryReader(inputstream);

        byte[] data = br.ReadBytes((Int32)inputstream.Length);

        string constr = "data source=localhost; initial catalog=sample; persist security    
        info=True; Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand("Insert_Images", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@Image", SqlDbType.VarBinary).Value = data;
        com.Parameters.Add("@imagename", SqlDbType.VarChar).Value = uploadedFileName;
        con.Open();
        int result = com.ExecuteNonQuery();
        con.Close();

       }


  plz suggest any answer to me

1 个答案:

答案 0 :(得分:0)

必须在任何插件之前引用jQuery库。它必须只参考一次。 更改script标记的顺序如下,并删除jquery-1.7.1.js文件参考

<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/fileuploader.js" type="text/javascript"></script>

在代码中查看以下行:

        using (inputstream)
        {
            byte[] buffer = new byte[16 * 1024];
            int bytesRead;

            while ((bytesRead = inputstream.Read(buffer, 0, buffer.Length)) > 0)
            {
                fileStream.Write(buffer, 0, bytesRead);
            }

        }

在这里,您将输入流中的数据写入文件,然后处理输入流。 将数据保存到所有位置后,需要处理输入流。 每次保存后,您必须在输入流中设置位置,以便开始调用inputStream.Seek(0, SeekOrigin.Begin)或明确设置位置:inputStream.Position = 0;