public async Task <APIResult> UploadFileToOSS(Stream stream, string path) { CreateOssClientOfIOS(); string key = path.LastIndexOf("/") != -1 ? path.Substring(path.LastIndexOf("/") + 1) : ""; //_ossClient.PutObject("vgic", key, path); //path = path.Substring(1); stream.Position = 0; var result = await Task <PutObjectResult> .Factory.FromAsync( _ossClient.BeginPutObject, _ossClient.EndPutObject, "vgic", key, stream, null); AttachDto attachDto = new AttachDto(); attachDto.AttachName = path.LastIndexOf("/") != -1 ? path.Substring(path.LastIndexOf("/") + 1) : ""; //var metadata = _ossClient.GetObjectMetadata("vgic", key); //var etag = metadata.ETag; //var req = new GeneratePresignedUriRequest("vgic", key, SignHttpMethod.Get); attachDto.Url = "http://vgic.oss-cn-beijing.aliyuncs.com/" + attachDto.AttachName; return(new APIResult { Body = CommonHelper.EncodeDto <AttachDto>(attachDto), ResultCode = ResultType.Success, Msg = "" }); }
public async Task <APIResult> Create(DocumentInput input) { var file = input.File; input.Name = file.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault(); var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); var parsedFilename = HeaderUtilities.RemoveQuotes(parsedContentDisposition.FileName); var filename = Guid.NewGuid().ToString() + Path.GetExtension(parsedFilename); if (!Directory.Exists("\\\\CNVFCUSTIF01.home.e-kmall.com" + "\\uploads\\")) { try { Directory.CreateDirectory("\\\\CNVFCUSTIF01.home.e-kmall.com" + "\\uploads\\"); } catch { } } var fileDestination = Path.Combine("\\\\CNVFCUSTIF01.home.e-kmall.com", "uploads", filename); var upload = new AttachDto() { AttachName = input.Name, Url = "http://fiat.qa.elandcloud.com/uploads/" + filename, SeqNo = input.Id }; using (var fileStream = new FileStream(fileDestination, FileMode.Create)) { var inputStream = file.OpenReadStream(); await inputStream.CopyToAsync(fileStream); //JsonConvert.SerializeObject(t) }; IEnumerable <AttachDto> resultInfo = new AttachDto[] { upload }; APIResult result = new APIResult { Body = CommonHelper.EncodeDto <AttachDto>(resultInfo), ResultCode = ResultType.Success, Msg = "" }; return(result); }
public async Task<APIResult> UploadFileToOSS(Stream stream, string filename, string extraParam) { if (null == stream) { return new APIResult { Body = "", ResultCode = ResultType.Success, Msg = "" }; } try { //Android上传文件突然报Aliyun.OSS 空引用的错误,所以修改了Android文件上传方法。 //if (App.SysOS == "Android") //{ // if (_ossClient == null) // CreateOssClient(); // string key = string.Empty; // if (extraParam.ToUpper() == "L") // { // key = _dir + filename; // } // else // { // key = _dir + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + filename.Substring(filename.LastIndexOf(".")); // } // //_ossClient.PutObject("vgic", key, path); // stream.Position = 0; // var result = await Task<PutObjectResult>.Factory.FromAsync( // _ossClient.BeginPutObject, // _ossClient.EndPutObject, // _bucket, key, stream, // null); // //var tcs = new TaskCompletionSource<bool>(); // //// 创建上传Object的Metadata // //ObjectMetadata meta = new ObjectMetadata(); // //// 上传Object. // //PutObjectResult result = _ossClient.PutObject(_bucket, key, stream, meta); // AttachDto attachDto = new AttachDto(); // attachDto.AttachName = filename; // attachDto.Url = _domain +key; // return new APIResult { Body = CommonHelper.EncodeDto<AttachDto>(attachDto), ResultCode = ResultType.Success, Msg = "" }; //} //else //{ HttpClient client = new HttpClient(); byte[] bytes = new byte[(int)stream.Length]; stream.Read(bytes, 0, bytes.Length); client.BaseAddress = new Uri(_config.Get<string>(Config.Config.Endpoints_UploadOSSBaseUrl)); StringContent sc = new StringContent(JsonConvert.SerializeObject(new { Bytes = bytes, Filename = filename, ExtraParam = extraParam }), Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("Excel/UploadToOss", sc); if (response.IsSuccessStatusCode) { AttachDto attachDto = new AttachDto(); attachDto.AttachName = filename; attachDto.Url = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result.ToString()).ToString(); return new APIResult { Body = CommonHelper.EncodeDto<AttachDto>(attachDto), ResultCode = ResultType.Success, Msg = "" }; } else { return new APIResult { Body = "", ResultCode = ResultType.Failure, Msg = "" }; } //} } catch (Exception e) { return new APIResult { Body = "", ResultCode = ResultType.Failure, Msg = e.StackTrace + "====" + e.ToString() }; } }
public JsonResult getAttachForQuotation(int id) { var sessione = (SessionModel)Session[Resources.Constante.SessionUsuario]; var quotation = quotationRepository.GetQuotation(id, SessionUsuario); DateTime fileCreationDatetime = DateTime.Now; string fileNameDocument = string.Format("{0}_{1}.pdf", quotation.Data.Code, fileCreationDatetime.ToString(@"yyyyMMdd") + "_" + fileCreationDatetime.ToString(@"HHmmss")); string pdfPathDocument = Server.MapPath(@"~\Documents\") + fileNameDocument; using (FileStream msReport = new FileStream(pdfPathDocument, FileMode.Create)) { //step 1 using (Document pdfDoc = new Document(PageSize.A4, 40, 40, 140, 40)) { try { // step 2 PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport); //pdfWriter.PageEvent = new ITextEvents(); pdfDoc.SetPageSize(PageSize.A4.Rotate()); //open the stream pdfDoc.Open(); var elements = generator.GetPageEight(quotation, sessione.FullName); pdfDoc.Add(elements); pdfDoc.NewPage(); pdfDoc.SetPageSize(PageSize.A4.Rotate()); pdfDoc.Close(); } catch (Exception ex) { //handle exception } finally { } } } //string pdf1 = Server.MapPath(@"~\Documents\archivo1.pdf") ; ////string pdf1 = Server.MapPath(@"~\Documents\") + fileNameDocument; string pdf2 = Server.MapPath(@"~\Template\PLANTILLA_PROPUESTA_COMERCIAL.pdf"); long lengthPdf1 = new FileInfo(pdfPathDocument).Length; long lengthPdf2 = new FileInfo(pdf2).Length; var list = new List <AttachDto>(); var AttachDto1 = new AttachDto(); AttachDto1.Name = fileNameDocument; AttachDto1.Size = lengthPdf1; list.Add(AttachDto1); var AttachDto2 = new AttachDto(); AttachDto2.Name = "PLANTILLA_PROPUESTA_COMERCIAL.pdf"; AttachDto2.Size = lengthPdf2; list.Add(AttachDto2); var _json = new SendMessage { attaches = list, FirstNameDocument = fileNameDocument, SecondNameDocument = "PLANTILLA_PROPUESTA_COMERCIAL.pdf" }; return(Json(_json, "application/json", Encoding.UTF8, JsonRequestBehavior.AllowGet)); }