Hi.
i have an ex. script for this Ajax code.
I have some questions for it, that i cant find.
- Where do i set the folder that i want to save the images to !?
- if I want to save the image name to an db where in the code_behind do i then add my savecode for db !?
Main_Page
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="imageupload.aspx.vb" Inherits="imageupload" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<style>
.ajax__fileupload_button
{
background-color: green;
}
</style>
<div>
<ajaxToolkit:ToolkitScriptManager runat="Server" EnablePartialRendering="true" ID="ScriptManager1" />
<script type="text/javascript">
function onClientUploadComplete(sender, e) {
var id = e.get_fileId();
onImageValidated("TRUE", e);
}
function onImageValidated(arg, context) {
var test = document.getElementById("testuploaded");
test.style.display = 'block';
var fileList = document.getElementById("fileList");
var item = document.createElement('div');
item.style.padding = '4px';
if (arg == "TRUE") {
var url = context.get_postedUrl();
url = url.replace('&', '&');
item.appendChild(createThumbnail(context, url));
} else {
item.appendChild(createFileInfo(context));
}
fileList.appendChild(item);
}
function createFileInfo(e) {
var holder = document.createElement('div');
holder.appendChild(document.createTextNode(e.get_fileName() + ' with size ' + e.get_fileSize() + ' bytes'));
return holder;
}
function createThumbnail(e, url) {
var holder = document.createElement('div');
var img = document.createElement("img");
img.style.width = '80px';
img.style.height = '80px';
img.setAttribute("src", url);
holder.appendChild(createFileInfo(e));
holder.appendChild(img);
return holder;
}
</script>
<div class="demoarea">
<div class="demoheading">
AjaxFileUpload Demonstration</div>
Click <i>Select File</i> to select an image file to upload. You can upload a maximum
of 10 jpeg files (files with the .jpg or .jpeg extension)
<br />
<asp:Label runat="server" ID="myThrobber" Style="display: none;"><img align="absmiddle" alt="" src="uploading.gif"/></asp:Label>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" Padding-Bottom="4"
Padding-Left="2" Padding-Right="1" Padding-Top="4" ThrobberID="myThrobber" OnClientUploadComplete="onClientUploadComplete"
OnUploadComplete="AjaxFileUpload1_OnUploadComplete" MaximumNumberOfFiles="10"
AllowedFileTypes="jpg,jpeg,png" />
<br />
<div id="testuploaded" style="display: none; padding: 4px; border: gray 1px solid;">
<h4>
list of uploaded files:</h4>
<hr />
<div id="fileList">
</div>
</div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</div>
</div>
</form>
</body>
</html>
Code_Behind
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports AjaxControlToolkit
Partial Class imageupload
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
' check if postback came through AjaxFileUpload control
' do for ajax file upload partial postback request
If AjaxFileUpload1.IsInFileUploadPostBack Then
' do for normal page request
Else
End If
If Request.QueryString("preview") = "1" AndAlso Not String.IsNullOrEmpty(Request.QueryString("fileId")) Then
Dim fileId = Request.QueryString("fileId")
Dim fileContents = DirectCast(Session("fileContents_" & fileId), Byte())
Dim fileContentType = DirectCast(Session("fileContentType_" & fileId), String)
If fileContents IsNot Nothing Then
Response.Clear()
Response.ContentType = fileContentType
Response.BinaryWrite(fileContents)
Response.[End]()
End If
End If
End Sub
Protected Sub AjaxFileUpload1_OnUploadComplete(sender As Object, file As AjaxFileUploadEventArgs)
' User can save file to File System, database or in session state
If file.ContentType.Contains("jpg") OrElse file.ContentType.Contains("gif") OrElse file.ContentType.Contains("png") OrElse file.ContentType.Contains("jpeg") Then
Session("fileContentType_" & Convert.ToString(file.FileId)) = file.ContentType
Session("fileContents_" & Convert.ToString(file.FileId)) = file.GetContents()
End If
' Set PostedUrl to preview the uploaded file.
file.PostedUrl = String.Format("?preview=1&fileId={0}", file.FileId)
End Sub
End Class