Код | protected void buttonOKClick (object sender, System.EventArgs e) { // Upload file to http server. Helper helper = new Helper (); // Is file can be upload (exist, correct size, etc). bool isUploadFail = false; string savePath = Request.PhysicalApplicationPath + @"App_Data/Upload/"; if (fileUpload.HasFile) { if (helper.IsValidExtension (fileUpload.FileName) ) { int fileSize = fileUpload.PostedFile.ContentLength; if (fileSize < ConstantProvider.MaxFileUploadSize) { savePath += fileUpload.FileName; fileUpload.SaveAs (savePath); labelUploadStatus.ForeColor = Color.Green; labelUploadStatus.Text = @"Файл передан на сервер."; } else { isUploadFail = true; labelUploadStatus.ForeColor = Color.Red; labelUploadStatus.Text = @"Размер файла превышает допустимый (8 Мб)."; } } else { isUploadFail = true; labelUploadStatus.ForeColor = Color.Red; labelUploadStatus.Text = @"Тип файла не соответствует допустимым."; } } // If user select file and was not problem. if (!isUploadFail) { string insertCommand = "insert into claim_appendix ("; if (fileUpload.HasFile) insertCommand += "data, " + "data_file_name, "; insertCommand += "claim_id) " + "values ("; if (fileUpload.HasFile) insertCommand += "@data, " + "@dataFileName, "; insertCommand += "@claimID);"; // Execute SQL-command to insert record in database. FbCommand command = new FbCommand (insertCommand); if (fileUpload.HasFile) { helper.CommandParameterAdd (command, "@data", savePath, FbDbType.Binary); helper.CommandParameterAdd (command, "@dataFileName", Path.GetFileName (savePath), FbDbType.VarChar); } helper.CommandParameterAdd (command, "@claimID", dropDownListClaimID.SelectedItem.Value, FbDbType.BigInt);
DataAccessLayer dataAccessLayer = GetDataAccessLayerForCurrentUser ("project_manager"); dataAccessLayer.ExecuteNonQuery (command); command.Dispose (); dataAccessLayer.Dispose (); // Delete inserted file from temporal directory. if (fileUpload.HasFile) File.Delete (savePath); dropDownListClaimAppendix.DataBind (); } }
|
Кусок рабочего кода, который для того, что бы сохранить файл в БД, сначала загружает его на сервер. |