dev uploadcontrol 上传图片

<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="width: 500px;padding-left:20px;">
<tr>
<td style="width: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="width: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="width: 500px">
<tr>
<td style="padding-right: 20px; vertical-align: top;"></td>
<td class="imagePreviewCell">
<img src="../Content/ImagePreview.gif" id="previewImage" alt="" style="width: 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;
}

上一篇:win2012R2安装net4.6.2失败提示“更新2919355包问题,或者win8.1、win10”的错误


下一篇:Table of Contents - MyBatis