[英]cannot upload images into database and application(fileuploader js file)? fileuploader js
我的要求是我必須使用fileuploaderjs文件更新數據庫和應用程序中的圖像。 但是我要在應用程序或數據庫中上傳。 下面是我的代碼,請幫幫我。我的數據庫名稱為Image,有兩列,分別為imagename,image。實際上,一旦我們讀取流數據,其中的內容就會變為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
必須在所有插件之前引用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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.