<script type="text/javascript">
// <![CDATA[
function Uploader_OnUploadStart() {
btnUpload.SetEnabled(false);
}
function Uploader_OnFileUploadComplete(args) {
var imgSrc = aspxPreviewImgSrc;
if (args.isValid) {
var date = new Date();
imgSrc = "/UploadImages/" + args.callbackData + "?dx=" + date.getTime();
}
getPreviewImageElement().src = imgSrc;
}
function Uploader_OnFilesUploadComplete(args) {
UpdateUploadButton();
}
function UpdateUploadButton() {
btnUpload.SetEnabled(uploader.GetText(0) != "");
}
function getPreviewImageElement() {
return document.getElementById("previewImage");
}
// ]]>
</script>
<script type="text/javascript">
// <![CDATA[
var aspxPreviewImgSrc = getPreviewImageElement().src;
// ]]>
</script>
<!--添加图片-->
<table style=" 500px;padding-left:20px;">
<tr>
<td style="300px">
<dx:ASPxUploadControl ID="uplImage" runat="server" ClientInstanceName="uploader" ShowProgressPanel="True"
NullText="Click here to browse files..." Size="35" OnFileUploadComplete="uplImage_FileUploadComplete">
<ClientSideEvents FileUploadComplete="function(s, e) { Uploader_OnFileUploadComplete(e); }"
FilesUploadComplete="function(s, e) { Uploader_OnFilesUploadComplete(e); }"
FileUploadStart="function(s, e) { Uploader_OnUploadStart(); }"
TextChanged="function(s, e) { UpdateUploadButton(); }"></ClientSideEvents>
<ValidationSettings MaxFileSize="4194304" AllowedFileExtensions=".jpg,.jpeg,.jpe,.gif,.png">
</ValidationSettings>
</dx:ASPxUploadControl>
</td>
<td style="100px">
<dx:ASPxButton ID="btnUpload" runat="server" AutoPostBack="False" Text="Upload" ClientInstanceName="btnUpload"
Width="100px" ClientEnabled="False" Style="margin: 0 auto;">
<ClientSideEvents Click="function(s, e) { uploader.Upload(); }" />
</dx:ASPxButton>
</td>
</tr>
</table>
<table style=" 500px">
<tr>
<td style="padding-right: 20px; vertical-align: top;"></td>
<td class="imagePreviewCell">
<img src="../Content/ImagePreview.gif" id="previewImage" alt="" style=" 490px; height: 200px;" /></td>
</tr>
</table>
后台代码:
string UploadDirectory = "/UploadImages/";
string ThumbnailFileName = Guid.NewGuid().ToString();
protected void uplImage_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
{
ThumbnailFileName += e.UploadedFile.FileName;
e.CallbackData = SavePostedFile(e.UploadedFile);
}
string SavePostedFile(UploadedFile uploadedFile)
{
if (!uploadedFile.IsValid)
return string.Empty;
string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
uploadedFile.SaveAs(fileName);
Session["pathpicture"] = UploadDirectory+ThumbnailFileName;
return ThumbnailFileName;
}