public static Boolean CheckFormatoFile(HttpPostedFileBase file, Models.TipoMedia formato = Models.TipoMedia.FOTO) { if (file != null && file.ContentLength > 0) { string estensione = new FileInfo(Path.GetFileName(file.FileName)).Extension; string nomeFileUnivoco = System.Guid.NewGuid().ToString() + estensione; FileMedia media = Models.VerificaUpload.getIstanziaFile(estensione.ToLower()); // formare stringa esadecimale string hexFile = string.Empty; Stream stream = file.InputStream; BinaryReader binary = new BinaryReader(stream); byte[] firmaFile = binary.ReadBytes(media.NumBytesID); foreach (byte B in firmaFile) { hexFile += B.ToString("X2"); } if (media != null && media.checkFormato(hexFile) && media.TipoFile == formato) { //log.Debug("Tipo file verificato."); return(true); } else { // lanciare eccezione file non corrispondente all'estensione throw new InvalidDataException(App_GlobalResources.Language.ErrorFormatFile); } } return(false); }
private void OnAddMediaButtonClick(object sender, EventArgs e) { if (myAddMediaOpenFileDialog.ShowDialog() == DialogResult.Cancel) { return; } var media = new FileMedia(myAddMediaOpenFileDialog.FileName); myVlcControl.Manager.MediaLibrary.MediaItems.Add(media); myMediasListBox.Items.Add(media); }
public HttpResponseMessage PostPhoto(FileMedia photo) { //System.Threading.Thread.Sleep(2000); // test api latency //throw new Exception("Oops"); // test unexpected server error // when the photo us null, it's because the user tried to upload an invalid file type (415) if (photo == null) throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); // do not allow photo uploads exceeding 1MB (413) if (photo.Content.Length > (1024 * 1024)) throw new HttpResponseException(HttpStatusCode.RequestEntityTooLarge); _photoUpdateHandler.Handle(new UpdateMyPhoto(User) { Name = photo.FileName, MimeType = photo.ContentType, Content = photo.Content, }); // for some reason, KendoUIWeb's upload widget will only think the upload succeeded // when the response is either empty, or contains a JSON payload with text/plain encoding. // so if we want to send a message back to the client, we have to serialize it in a JSON wrapper. const string successMessage = "Your photo was changed successfully."; var successPayload = new { message = successMessage }; var successJson = JsonConvert.SerializeObject(successPayload); return Request.CreateResponse(HttpStatusCode.OK, successJson, "text/plain"); }