Javascript 无法将图像上载到数据库和应用程序(fileuploader js文件)?文件上传程序js
我的要求是我必须使用fileuploaderjs文件更新数据库和应用程序中的图像。但我要在应用程序或数据库中上传。下面是我的代码,请帮助我。我的名为Images的数据库有两列,即imagename,image。实际上,一旦我们读取流数据,其中的内容将变为null。因此,我无法进一步使用该流。。这就是为什么我不能同时在数据库和应用程序文件夹中上载。我可以在数据库或应用程序文件夹中上载 .cshtmlJavascript 无法将图像上载到数据库和应用程序(fileuploader js文件)?文件上传程序js,javascript,asp.net-mvc-4,file-upload,Javascript,Asp.net Mvc 4,File Upload,我的要求是我必须使用fileuploaderjs文件更新数据库和应用程序中的图像。但我要在应用程序或数据库中上传。下面是我的代码,请帮助我。我的名为Images的数据库有两列,即imagename,image。实际上,一旦我们读取流数据,其中的内容将变为null。因此,我无法进一步使用该流。。这就是为什么我不能同时在数据库和应用程序文件夹中上载。我可以在数据库或应用程序文件夹中上载 .cshtml <link href="../../Content/fileuploader.css" re
<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
$(文档).ready(函数(){
警惕(‘in’);
var uploader=new qq.FileUploader({
//传递dom节点(对于jQuery用户,例如$(选择器)[0]
元素:document.getElementById('file-uploader-demo1'),
操作:'/Home/DigitalAssetFileUpload?文件类型=图像',
模板:“”+
“
”+
“上载文件”+
'',
多重:假,
允许的扩展:['jpg','png','gif'],
是的,
onComplete:函数(id、文件名、responseJSON){
警报(“插入”);
}
});
});
**控制器类**
这是我的控制器。
public void digitalAssetFileUpload(字符串recFileType=“image”)
{
长fileSizeInBytes=0;
string fileType=HttpContext.Request.QueryString[“fileType”];
字符串phyicalFilePath=Server.MapPath(“~/Images”);
string uploadedFileName=HttpContext.Request.Headers[“X-File-Name”];
uploadedFileName=HttpUtility.UrlDecode(uploadedFileName);
字符串fileExt=Path.GetExtension(uploadedFileName);
流inputstream=null;
inputstream=HttpContext.Request.inputstream;
字符串storagePath=phyicalFilePath;
如果(!Directory.Exists(storagePath))
{
System.IO.Directory.CreateDirectory(storagePath);
}
字符串pFilePath=storagePath+“\\”+uploadedFileName;
FileStream FileStream=newfilestream(pFilePath,FileMode.OpenOrCreate);
如果(inputstream==null)
{
System.Diagnostics.Debug.WriteLine(“应用程序”,“流为空”);
抛出新的NullReferenceException(“流为null”);
}
fileSizeInBytes=inputstream.Length;
使用(文件流)
{
使用(输入流)
{
字节[]缓冲区=新字节[16*1024];
int字节读取;
而((bytesRead=inputstream.Read(buffer,0,buffer.Length))>0)
{
写入(缓冲区,0,字节读取);
}
}
}
BinaryReader br=新的BinaryReader(inputstream);
byte[]data=br.ReadBytes((Int32)inputstream.Length);
string constr=“数据源=本地主机;初始目录=示例;持久化安全性
信息=真;集成安全=SSPI”;
SqlConnection con=新的SqlConnection(cont);
SqlCommand com=新的SqlCommand(“插入图像”,con);
com.CommandType=CommandType.StoredProcess;
Add(“@Image”,SqlDbType.VarBinary).Value=data;
Add(“@imagename”,SqlDbType.VarChar).Value=uploadedFileName;
con.Open();
int result=com.ExecuteNonQuery();
con.Close();
}
请告诉我答案
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>
在这里,您可以将数据从输入流写入文件,然后再处理输入流。
在将数据保存到所有位置后,需要处理输入流。
每次保存后,您必须将输入流中的位置设置为开始调用
inputStream.Seek(0,请参见korigin.Begin)
或显式设置位置:inputStream.position=0代码>谢谢您的评论。。但这并不能解决我的问题,只能在应用程序文件夹中上传,不能在数据库中上传。数据库中的图像数据为空
using (inputstream)
{
byte[] buffer = new byte[16 * 1024];
int bytesRead;
while ((bytesRead = inputstream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, bytesRead);
}
}