Tab

UploadedFile.SaveAs(String) Method

Saves the uploaded file specifying the full path on the server.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v20.2.dll

Declaration

public void SaveAs(
    string fileName
)
Public Sub SaveAs(
    fileName As String
)

Parameters

Name Type Description
fileName String

A String value that specifies the full path to the location on the server to save the uploaded file.

Remarks

Uploading a file specified on the client can be initiated either by the client ASPxClientUploadControl.Upload method, using the upload button or automatically, on the next round trip to the server. The SaveAs method can be used to save the uploaded file to a specific location on the server. Typically, the SaveAs method is called within the ASPxUploadControl.FileUploadComplete event's handler. Before calling the SaveAs method, it is recommended to test the FileUploadCompleteEventArgs.IsValid property, to verify whether the uploaded file passes the validation criteria if any have been defined via the ASPxUploadControl.ValidationSettings property or specific custom code.

The SaveAs method's parameter value represents the full path on the server where the uploaded file should be saved. This path can either be absolute (for instance, "C:/Documents/Image.jpg") or relative to the web application folder (by using the MapPath method). When composing the saved file's path, you can define any custom name for the file or use the name obtained by the UploadedFile.FileName property.

NOTE

To be able to save the uploaded file, the ASP.NET application should have write access to the specified directory on the server. If the full path file is not indicated, the file will be saved to the current location. If the file with the specified name already exists, this file will be overwritten.

IMPORTANT

The SaveAs method is not in effect if the file hasn't been uploaded to the server because of specific limitations defined within the web.config file's httpRuntime section; in particular, the maxRequestLength and executionTimeout attribute settings may affect file uploading. You can define the values of these attributes using the "Set Uploading Properties in web.config" dialog, available via the ASPxUploadControl's smart tag.

IMPORTANT

To use the SaveAs method, set the ASPxUploadControl.UploadStorage property to NotSet. Otherwise, files are saved according to predefined settings automatically and the SaveAs method has no effect.

Examples

The following part of the ASPxUploadControl Multi-File Upload online demo illustrates the ASPxUploadControl's capability to upload more than one file at a time.

<script runat="server">
 public partial class UploadControl_MultiFileUpload : BasePage
 {
     const string UploadDirectory = "~/UploadControl/UploadImages/";
     const int ThumbnailSize = 100;

     protected void Page_Load(object sender, EventArgs e) {
         if(IsPostBack)
             UploadControl.ShowProgressPanel = chbShowProgressPanel.Checked;
     }

     protected string SavePostedFiles(UploadedFile uploadedFile) {
         string ret = "";
         if(uploadedFile.IsValid) {
             FileInfo fileInfo = new FileInfo(uploadedFile.FileName);
             string resFileName = MapPath(UploadDirectory) + fileInfo.Name;
             uploadedFile.SaveAs(resFileName);

             string fileLabel = fileInfo.Name;
             string fileType = uploadedFile.PostedFile.ContentType.ToString();
             string fileLength = uploadedFile.PostedFile.ContentLength / 1024 + "K";
             ret = string.Format("{0} <i>({1})</i> {2}|{3}", 
                 fileLabel, fileType, fileLength, fileInfo.Name);
         }
         return ret;
     }
     protected void UploadControl_FileUploadComplete(object sender, 
      FileUploadCompleteEventArgs e) {
         try {
             e.CallbackData = SavePostedFiles(e.UploadedFile);
         }
         catch(Exception ex) {
             e.IsValid = false;
             e.ErrorText = ex.Message;
         }
     }
 }
</script>
...
         <dxuc:ASPxUploadControl ID="UploadControl" runat="server" 
             ShowAddRemoveButtons="True" Width="300px"
             ShowUploadButton="True" AddUploadButtonsHorizontalPosition="Left" 
             ShowProgressPanel="True" ClientInstanceName="UploadControl" 
             OnFileUploadComplete="UploadControl_FileUploadComplete" FileInputCount="3">
             <ValidationSettings MaxFileSize="4000000" 
             AllowedFileExtensions=".jpg,.jpe,.jpeg,.gif"></ValidationSettings>
             <ClientSideEvents
                 fileuploadcomplete="function(s, e) { FileUploaded(s, e) }"
                 fileuploadstart="function(s, e) { FileUploadStart(); }"
             />
         </dxuc:ASPxUploadControl>
...
         <dxrp:ASPxRoundPanel ID="ASPxRoundPanel1" runat="server" Width="300px" 
             ClientInstanceName="RoundPanel"
             HeaderText="Uploaded files (jpeg, gif)" Height="100%">
             <panelcollection>
                 <dxp:PanelContent runat="server">
                     <div id="uploadedListFiles" style="height: 150px; font-family: Arial;">
                     </div>
                 </dxp:PanelContent>
             </panelcollection>
         </dxrp:ASPxRoundPanel>
...
var fieldSeparator = "|";
function FileUploadStart() {
    document.getElementById("uploadedListFiles").innerHTML = "";
}
function FileUploaded(s, e) {
    if(e.isValid) {
        var linkFile = document.createElement("a");
        var indexSeparator = e.callbackData.indexOf(fieldSeparator);
        var fileName = e.callbackData.substring(0, indexSeparator);
        var pictureUrl = e.callbackData.substring(indexSeparator + fieldSeparator.length);
        var date = new Date();
        var imgSrc = "UploadImages/" + pictureUrl + "?dx=" + date.getTime();
        linkFile.innerHTML = fileName;
        linkFile.setAttribute("href", imgSrc);
        linkFile.setAttribute("target", "_blank");
        var container = document.getElementById("uploadedListFiles");
        container.appendChild(linkFile);
        container.appendChild(document.createElement("br"));
    }
}
See Also