public async Task <HttpResponseMessage> UploadToBlob() { string squestion_id = "questionId"; string sanswerId = "answerId"; string stitle = "title"; string bmaturity = "maturity"; int assessmentId = Auth.AssessmentForUser(); string fileHash; try { FileUploadStream fileUploader = new FileUploadStream(); Dictionary <string, string> formValues = new Dictionary <string, string>(); formValues.Add(squestion_id, null); formValues.Add(sanswerId, null); formValues.Add(stitle, null); formValues.Add(bmaturity, null); FileUploadStreamResult streamResult = await fileUploader.ProcessUploadStream(this.Request, formValues); // Find or create the Answer for the document to be associated with. // If they are attaching a document to a question that has not yet been answered, // the answerId will not be sent in the request. int questionId = int.Parse(streamResult.FormNameValues[squestion_id]); int answerId; bool isAnswerIdProvided = int.TryParse(streamResult.FormNameValues[sanswerId], out answerId); string title = streamResult.FormNameValues[stitle]; bool isMaturity = false; bool.TryParse(streamResult.FormNameValues[bmaturity], out isMaturity); if (!isAnswerIdProvided) { QuestionsManager qm = new QuestionsManager(assessmentId); Answer answer = new Answer(); answer.QuestionId = questionId; answer.AnswerText = "U"; answer.QuestionType = isMaturity ? "Maturity" : ""; answerId = qm.StoreAnswer(answer); } var dm = new DocumentManager(assessmentId); using (CSET_Context db = new CSET_Context()) { dm.AddDocument(title, answerId, streamResult); } // Return a current picture of this answer's documents return(Request.CreateResponse(dm.GetDocumentsForAnswer(answerId))); } catch (System.Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e)); } }
public async Task <int> UploadReferenceDoc() { try { FileUploadStream fileUploader = new FileUploadStream(); Dictionary <string, string> formValues = new Dictionary <string, string>(); formValues.Add("title", null); formValues.Add("setName", null); FileUploadStreamResult streamResult = await fileUploader.ProcessUploadStream(this.Request, formValues); // Create a GEN_FILE entry, and a SET_FILES entry. ModuleBuilderManager m = new ModuleBuilderManager(); return(m.RecordDocInDB(streamResult)); } catch (System.Exception e) { throw e; // throw Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } }
/// <summary> /// ファイルアップロード /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void FileUploadButton_Click(object sender, RoutedEventArgs e) { AsyncClientStreamingCall <FileUploadStream, FileUploadResponse> _callFileUpload; string filePath; string fileName; const int BufferSize = 10240; byte[] bin = new byte[BufferSize]; this._uploadCanceled = false; OpenFileDialog openFileDialog = new OpenFileDialog(); var dlg = openFileDialog.ShowDialog(); if (dlg == false) { return; } filePath = openFileDialog.FileName; fileName = System.IO.Path.GetFileName(filePath); this.FileUploadButton.IsEnabled = false; this.CancelUploadButton.IsEnabled = true; this.xUploadMessage.Text = fileName + " アップロード中"; // gRPC メッセージ 宣言 FileUploadStream fileUploadStream = new FileUploadStream(); fileUploadStream.FileName = fileName; fileUploadStream.FileSize = BufferSize; // gRPC サービスを呼び出す。 _callFileUpload = this.grpcClient.GreeterClient.FileUpload(); using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { int sendSize = 0; int readSize = 0; while ((readSize = fs.Read(bin, 0, BufferSize)) > 0) { if (this._uploadCanceled) { break; } fileUploadStream.Binary = Google.Protobuf.ByteString.CopyFrom(bin); await _callFileUpload.RequestStream.WriteAsync(fileUploadStream); await Task.Delay(TimeSpan.FromSeconds(1)); this.xUploadMessage.Text = fileName + " アップロード中 / Send Byte=" + (sendSize += readSize); } await _callFileUpload.RequestStream.CompleteAsync(); } this.FileUploadButton.IsEnabled = true; this.CancelUploadButton.IsEnabled = false; if (this._uploadCanceled) { this.xUploadMessage.Text = "キャンセルしました"; } else { this.xUploadMessage.Text = "Result = " + _callFileUpload.ResponseAsync.Result.Result.ToString(); } }