C# Github Api uploading a pdf file does not work - c#

Im trying to upload a pdf file using the octokit nuget package in c#.
But whenever i try to upload something i have to convert it to a string or i will get an error. If i convert the pdf to a string im able to upload it to my repo, but then its not a valid pdf file.
Im using this code: gitHubClient.Repository.Content.CreateFile(owner, repoName, filepath, new CreateFileRequest($".", File.ReadAllBytes(#"C:/Temp/CUSTOM.pdf").ToString(), branch));
Could someone please help me, i tried researching but i either find old articles or once which only upload a text file.

Related

ASP.NET: Modify and download docx file

I have a docx file which I would like to modify according to web user input. So, after the user submits the form on the web page, I need to modify the original docx file, and then download it to the user.
I try to store the original file as a resource file in my project, but I can't open it programatically.
That's what I tried in the post controller:
using Microsoft.Office.Interop.Word;
...
Application app = new Application();
Document document = app.Documents.Open(Properties.Resources.___, ReadOnly: false);
But I received a System.Runtime.InteropServices.COMException (0x80020005 (DISP_E_TYPEMISMATCH))
I also don't know how to download the modified file. In a simple console application document.SaveAs2(newPath); worked, but it doesn't seem to work for downloading.
(I'm not even sure that this whole way could work. If I can't use Microsoft.Office.Interop.Word this way, please let me know.)
If you want to manipulate docx files it's best to use the OpenXML API rather than the InterOP. See https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk?redirectedfrom=MSDN

Upload video to SharePoint library by code

I am making a app for SharePoint and have a form with several inputs for text and for files as well. The input for files works fine for regular files such as Word, Excel, PowerPoint, images etc... but wont work with videos. Is it possible to upload a video to a document library by code? Or cant you do that simply because the files is usually to big and would take long to upload?
The files is of the type HttpPostedFileBase and sent by a normal mvc form.
And this is the code for uploading it to the folder
FileCreationInformation attachFileInfo = new FileCreationInformation();
attachFileInfo.ContentStream = pNewRequest.FileToUpload1.InputStream;
attachFileInfo.Url = Path.GetFileName(pNewRequest.FileToUpload1.FileName);
newFolder.Files.Add(attachFileInfo);
According to this StackExchange answer, SP videos are organized in special VideoSet folders which seem to be similar to doc sets.
You said you're using MVC to submit the files so I'm assuming you're using CSOM on an external server. Here's an example for handling video uploads via CSOM.

how to read the content of PDF file which is opened in the browser

I have a requirement of reading the content of PDF file from browser using c# and then save it locally. There is no physical existence to that file. It is served when file code is provided using the query string. The sample url is like http://something/ReportStatus.aspx?indvl_pk=12334. I am getting no way to do that. Any help is appreciated. Thanks is advance.

error when upload excel file

Error when I try to upload Excel file . this only happened in my Edit page (HttpPost).
Hope I can get some solutions and explainations here.
Thanks.
Developing using MVC and C#.

WebClient.DownloadFile File Corrupt

I'm trying to download files directly from a list of urls.
I was able to download most files successfully except for the .docx. I was able to download the .docx file, but when I try to open it, the error message shows that "The file is corrupt and cannot be opened", when I try to repair it with Microsoft Word, I got another error message saying "Microsoft Office cannot open this file because some parts are missing or invalid". I don't have any issue when download pdf files.
My code is very simple and it looks like this:
WebClient webClient = new WebClient();
webClient.DownloadFile("http://somehost/somefile.docx", "C:\\somefolder\\somefile.docx");
webClient.Dispose(); //I added this line just to see if it will fix the problem but no it didn't
I went to the urls in the browser and make sure that the files does exist and are not corrupted. The urls are just fine and I was able to download files directly from the urls in a browser and the file opens.
Additional Information:
I did find one thing that's different for pdf url and docx url, but I really don't think it has anything to do with my problem. When I go to the pdf url in a browser, the pdf was displayed in the browser. However, when I go to the docx url, the page doesn't show anything, but the download for the file automatically starts. I don't think this will make a difference but just FYI.
EDIT 10:38AM
I just tried the Async method. I was able to download the docx file and open it, but it appear as a blank word document, which is still not correct. The same docx file I download from the browser does have content.
webClient.DownloadFileAsync(new Uri("http://somehost/somefile.docx"),"C:\\somefolder\\somefile.docx");
DownloadFileAsync downloads file in background, and your application probably terminates before download is completed.
You should wait for DownloadFileCompleted event, or use a DownloadFile method that will wait until file will be downloaded.
Thank you everyone for trying to help, I really appreciate it.
I realize that the problem was actually me not concatenating the url correctly. Right, a stupid mistake I made...
WebClient didn't throw error for incorrect format (for whatever reason), and my log file was not logging the actual url that I was trying to connect to, so I didn't realize it was doing the wrong thing.
Anyway, thank you all for the help and the comments that help me figure out what the problem was.

Categories

Resources