/// <summary> /// Creates and returns a new <see cref="UploadStorageConfig"/> (or /// subclass) for use with the current request. /// </summary> /// <returns>a new <see cref="UploadStorageConfig"/> or subclass.</returns> /// <remarks>If the installed module does not explicitly support /// creating <see cref="UploadStorageConfig"/> objects, this method returns /// the <see cref="UploadStorageConfig"/> created by the currently selected /// <see cref="UploadStorageProvider"/>.</remarks> public static UploadStorageConfig CreateUploadStorageConfig() { UploadStorageConfig storageConfig = InstalledModule.CreateUploadStorageConfig(); if (storageConfig == null) { storageConfig = UploadStorage.CreateUploadStorageConfig(); } return(storageConfig); }
/// <summary> /// Converts an <see cref="HttpPostedFile"/> to an <see cref="UploadedFile"/> /// that is associated with a particular control. /// </summary> /// <param name="controlUniqueID"> /// The UniqueID of the control with which the returned <see cref="UploadedFile"/> /// should be associated. If the <see cref="UploadedFile"/> is added to an /// <see cref="UploadedFileCollection"/>, the UniqueID can be used to retrieve /// it. /// </param> /// <param name="file"> /// The <see cref="HttpPostedFile"/> to convert to an <see cref="UploadedFile"/>. /// </param> /// <returns> /// The <see cref="UploadedFile"/> that corresponds to the <see cref="HttpPostedFile"/>. /// </returns> /// <remarks>If an <see cref="IUploadModule"/> is not installed or it does not support /// conversion, this method will delegate to <see cref="UploadStorage.ConvertToUploadedFile"/> /// which will use the currently configured <see cref="UploadStorageProvider"/> /// </remarks> public static UploadedFile ConvertToUploadedFile(string controlUniqueID, HttpPostedFile file) { UploadedFile uploadedFile = null; if (InstalledModule != null) { uploadedFile = InstalledModule.ConvertToUploadedFile(controlUniqueID, file); } if (uploadedFile == null) { uploadedFile = UploadStorage.ConvertToUploadedFile(controlUniqueID, file); } return(uploadedFile); }
public static UploadedFile ConvertToUploadedFile(string controlUniqueID, HttpPostedFile file) { // We use a temporary UploadContext so that we have something we can pass to the // UploadStorageProvider. Note that unlike when the UploadHttpModule is used, // this temporary context is not shared between uploaded files. UploadContext ctx = new UploadContext(); ctx._ContentLength = HttpContext.Current.Request.ContentLength; UploadStorageConfig storageConfig = UploadStorage.CreateUploadStorageConfig(); string storageConfigString = HttpContext.Current.Request.Form[Constants.ConfigNamePrefix + "-" + controlUniqueID]; if (storageConfigString != null && storageConfigString != string.Empty) { storageConfig.Unprotect(storageConfigString); } UploadedFile uploadedFile = UploadStorage.CreateUploadedFile(ctx, controlUniqueID, file.FileName, file.ContentType, storageConfig); System.IO.Stream outStream = null, inStream = null; try { outStream = uploadedFile.CreateStream(); inStream = file.InputStream; byte[] buf = new byte[4096]; int bytesRead = -1; while (outStream.CanWrite && inStream.CanRead && (bytesRead = inStream.Read(buf, 0, buf.Length)) > 0) { outStream.Write(buf, 0, bytesRead); } } finally { if (inStream != null) { inStream.Close(); } if (outStream != null) { outStream.Close(); } } return(uploadedFile); }
UploadedFile IUploadModule.ConvertToUploadedFile(string controlUniqueID, HttpPostedFile file) { return(UploadStorage.ConvertToUploadedFile(controlUniqueID, file)); }