Wednesday 27 June 2012

how to file upload in webmatrix

Uploading files looks quite similar to the FileUpload control and the File class in .NET. You can define an upload control in the page like this:
@FileUpload.GetHtml
(
  initialNumberOfFiles:1, 
  allowMoreFilesToBeAdded:false, 
  includeFormTag:true, 
  uploadText:"Upload")
  @if (IsPost) {
    <span>File uploaded!</span><br/>
}     
To save the file, you can use code like this:
@{  
  var fileName = "";
  if (IsPost) {
    var fileSavePath = "";
    var uploadedFile = Request.Files[0];
    fileName = Path.GetFileName(uploadedFile.FileName);
    fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" + fileName);
    uploadedFile.SaveAs(fileSavePath);
  } 
}
Notice how properties such as initialNumberOfFiles and allowMoreFilesToBeAdded make it easy to change the client side behavior of the upload page. Set allowMoreFilesToBeAdded to true and the user gets an Add button to upload multiple files at once, shown in figure 7:


Besides uploading files, WebMatrix also makes it easy to work with images. For example, you can scale and flip images and there is built-in support for adding watermarks, something that previously required third party controls or a fair bit of custom code.

No comments:

Post a Comment